1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 15:23:05 +04:00

Merge pull request #28535 from AhmadDurrani579:4.x

videoio(gstreamer): fix timestamp drift and color negotiation on Apple #28535

This commit addresses two issues on macOS with Apple M3 hardware:
1. Replaces floating-point timestamp math with gst_util_uint64_scale_int to ensure nanosecond precision.
2. Explicitly forces I420 format in the encoding profile to prevent hardware encoder negotiation failure.

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [ ] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Ahmad
2026-04-02 11:59:01 +01:00
committed by GitHub
parent 2b31e057cf
commit 10e32c96f5
2 changed files with 34 additions and 14 deletions
@@ -233,6 +233,7 @@ enum VideoWriterProperties {
VIDEOWRITER_PROP_KEY_FLAG = 11, //!< Set to non-zero to signal that the following frames are key frames or zero if not, when encapsulating raw video (\ref VIDEOWRITER_PROP_RAW_VIDEO != 0). FFmpeg back-end only.
VIDEOWRITER_PROP_PTS = 12, //!< Specifies the frame presentation timestamp for each frame using the FPS time base. This property is **only** necessary when encapsulating **externally** encoded video where the decoding order differs from the presentation order, such as in GOP patterns with bi-directional B-frames. The value should be provided by your external encoder and for video sources with fixed frame rates it is equivalent to dividing the current frame's presentation time (\ref CAP_PROP_POS_MSEC) by the frame duration (1000.0 / VideoCapture::get(\ref CAP_PROP_FPS)). It can be queried from the resulting encapsulated video file using VideoCapture::get(\ref CAP_PROP_PTS). FFmpeg back-end only.
VIDEOWRITER_PROP_DTS_DELAY = 13, //!< Specifies the maximum difference between presentation (pts) and decompression timestamps (dts) using the FPS time base. This property is necessary **only** when encapsulating **externally** encoded video where the decoding order differs from the presentation order, such as in GOP patterns with bi-directional B-frames. The value should be calculated based on the specific GOP pattern used during encoding. For example, in a GOP with presentation order IBP and decoding order IPB, this value would be 1, as the B-frame is the second frame presented but the third to be decoded. It can be queried from the resulting encapsulated video file using VideoCapture::get(\ref CAP_PROP_DTS_DELAY). Non-zero values usually imply the stream is encoded using B-frames. FFmpeg back-end only.
VIDEOWRITER_PROP_COLOR_SPACE = 14, //!< (**open-only**) GStreamer backend only. Pixel format for the encoding profile. Default is "I420". Other values: "NV12", "BGRx". See GStreamer raw video formats for more options.
#ifndef CV_DOXYGEN
CV__VIDEOWRITER_PROP_LATEST
#endif
+33 -14
View File
@@ -2512,25 +2512,43 @@ bool CvVideoWriter_GStreamer::open( const std::string &filename, int fourcc,
CV_WARN("OpenCV backend does not support this file type (extension): " << filename);
return false;
}
//create pipeline elements
encodebin.reset(gst_element_factory_make("encodebin", NULL));
if (!encodebin)
{
CV_WARN("GStreamer: cannot create encodebin element");
return false;
}
GSafePtr<GstCaps> containercaps;
GSafePtr<GstEncodingContainerProfile> containerprofile;
GSafePtr<GstEncodingVideoProfile> videoprofile;
containercaps.attach(gst_caps_from_string(mime));
//create encodebin profile
containerprofile.attach(gst_encoding_container_profile_new("container", "container", containercaps.get(), NULL));
videoprofile.reset(gst_encoding_video_profile_new(videocaps.get(), NULL, NULL, 1));
gst_encoding_container_profile_add_profile(containerprofile.get(), (GstEncodingProfile*)videoprofile.get());
unsigned int colorspace_fourcc = (unsigned int)params.get(VIDEOWRITER_PROP_COLOR_SPACE, CV_FOURCC('I', '4', '2', '0'));
const char* colorspace = gst_video_format_to_string(gst_video_format_from_fourcc(colorspace_fourcc));
GSafePtr<GstCaps> prof_caps;
std::string caps_str = std::string("video/x-raw, format=") + std::string(colorspace);
prof_caps.attach(gst_caps_from_string(caps_str.c_str()));
videoprofile.attach(gst_encoding_video_profile_new(prof_caps.get(), NULL, NULL, 1));
// Transfer ownership to the container profile
gst_encoding_container_profile_add_profile(
containerprofile.get(),
(GstEncodingProfile*)videoprofile.detach()
);
g_object_set(G_OBJECT(encodebin.get()), "profile", containerprofile.get(), NULL);
source.reset(gst_element_factory_make("appsrc", NULL));
if (!source)
{
CV_WARN("GStreamer: cannot create appsrc element");
return false;
}
file.reset(gst_element_factory_make("filesink", NULL));
if (!file)
{
CV_WARN("GStreamer: cannot create filesink element");
return false;
}
g_object_set(G_OBJECT(file.get()), "location", (const char*)filename.c_str(), NULL);
}
@@ -2709,21 +2727,22 @@ void CvVideoWriter_GStreamer::write(InputArray image)
Mat imageMat = image.getMat();
const size_t buf_size = imageMat.total() * imageMat.elemSize();
duration = ((double)1/framerate) * GST_SECOND;
timestamp = num_frames * duration;
duration = gst_util_uint64_scale_int(GST_SECOND, 1, framerate);
timestamp = gst_util_uint64_scale_int(num_frames, GST_SECOND, framerate);
//gst_app_src_push_buffer takes ownership of the buffer, so we need to supply it a copy
GstBuffer *buffer = gst_buffer_new_allocate(NULL, buf_size, NULL);
GstMapInfo info;
gst_buffer_map(buffer, &info, (GstMapFlags)GST_MAP_READ);
memcpy(info.data, (guint8*)imageMat.data, buf_size);
gst_buffer_unmap(buffer, &info);
if (gst_buffer_map(buffer, &info, (GstMapFlags)GST_MAP_WRITE)) {
memcpy(info.data, (guint8*)imageMat.data, buf_size);
gst_buffer_unmap(buffer, &info);
}
GST_BUFFER_DURATION(buffer) = duration;
GST_BUFFER_PTS(buffer) = timestamp;
GST_BUFFER_DTS(buffer) = timestamp;
//set the current number in the frame
GST_BUFFER_OFFSET(buffer) = num_frames;
GST_BUFFER_OFFSET_END(buffer) = num_frames + 1;
ret = gst_app_src_push_buffer(GST_APP_SRC(source.get()), buffer);
if (ret != GST_FLOW_OK)
{