Commit Graph
68 Commits
Author SHA1 Message Date
Isaac ConnorandClaude Opus 4.7 bfe9c4ad4a fix: free previously-owned buffer in AssignDirect(AVFrame); nullptr-safe logs (PR #4788)
- Image::AssignDirect(const AVFrame*): both the invalidate() failure
  path and the success path overwrote buffer/buffertype without freeing
  any previously-owned buffer. If the Image currently owned a
  ZM_BUFTYPE_ZM/MALLOC/NEW allocation, that memory was leaked on every
  AssignDirect(frame) call. Added DumpImgBuffer() at the top of
  invalidate() and immediately before the success-path buffer
  reassignment; DumpBuffer is a no-op for DONTFREE buffers, so this is
  safe whether the Image previously owned its memory or wrapped a
  caller's buffer.
- Switched five remaining av_get_pix_fmt_name() calls flagged by review
  to zm_get_pix_fmt_name():
  * Monitor::GetAlarmImage warnings (zm_monitor.cpp:1432, 1438-1439)
  * Monitor::ReadShmFrame warnings (zm_monitor.cpp:3042, 3049-3050)
  * Camera ctor fallback Error (zm_camera.cpp:90)
  All can be reached with AV_PIX_FMT_NONE or unrecognised formats where
  av_get_pix_fmt_name returns nullptr, which is undefined when fed to %s.

refs #4788

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-02 21:39:16 -04:00
Isaac ConnorandClaude Opus 4.7 133bb53c77 fix: Camera::linesize/imagesize back to tightly-packed (PR #4788)
Camera::linesize/imagesize was changed by the migration to FFALIGN(.,32)
and av_image_get_buffer_size(...,32) to keep SIMD-friendly strides. But
Camera describes the *device-side* buffer that capture paths copy from
(V4L2 mmap buffers, raw RTP frames, etc.). Those external buffers are
tightly packed at the driver/source stride, not 32-byte aligned, so the
inflated imagesize causes:
- LocalCamera::PrimeCapture's `pSize != imagesize` check
  (av_image_get_buffer_size(..., align=1) vs Camera::imagesize) to
  Fatal for any width that isn't a multiple of 32.
- Other raw-socket capture paths to pass an oversized imagesize as the
  source buffer size, risking out-of-bounds reads of driver-allocated
  buffers.

Revert Camera::linesize/imagesize to align=1 (tightly packed). Image
internal buffers and SHM slots independently apply 32-byte alignment
where they need it (Monitor::connect already takes
max(camera->ImageSize(), av_image_get_buffer_size(RGBA, w, h, 32)) for
the SHM slot, so the SIMD-aligned slot capacity is preserved).

refs #4788

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27 10:42:38 -04:00
Isaac ConnorandClaude Opus 4.7 1fe29202f5 fix: address PR #4788 review comments on AVPixelFormat migration
- Image::Assign: when source linesize > destination linesize, copy only
  the destination's row capacity per line; previously copied
  image.linesize bytes into rows of smaller linesize, overflowing the
  destination buffer on the last row.
- Monitor::CheckSignal: dispatch the 1-byte-per-pixel branch via
  zm_bytes_per_pixel(pix_fmt) == 1 so YUV422P/J422P are sampled on the
  Y plane like GRAY8/YUV420P, instead of falling through and reporting
  "no signal".
- Monitor::WriteShmFrame: record image_pixelformats[index] via
  capture_image->PixFormat() (canonical imagePixFormat) instead of
  AVPixFormat(), which re-derives from the deprecated
  (colours, subpixelorder) pair and could propagate stale metadata.
- Monitor::connect: rewrite stale comment that claimed readers don't
  need per-slot pixformat adoption; readers MUST call ReadShmFrame()
  to adopt the actual format zmc wrote.
- Camera::Camera: guard linesize/imagesize derivation against
  AV_PIX_FMT_NONE and negative returns from av_image_get_linesize /
  av_image_get_buffer_size, falling back to width * colours stride so
  unsigned wrap-around can't break SHM sizing.

refs #4788

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 19:36:20 -04:00
Isaac Connor 7b21371083 Reapply "fix: replace ZM_COLOUR system with AVPixelFormat for format dispatch"
This reverts commit 60b6b37c60.
2026-05-03 12:24:43 -04:00
Isaac Connor 60b6b37c60 Revert "fix: replace ZM_COLOUR system with AVPixelFormat for format dispatch" 2026-05-02 18:26:48 -04:00
Isaac ConnorandClaude Opus 4.6 49498f4076 fix: compute Camera linesize/imagesize from pixelFormat, not hardcoded YUV420P
linesize and imagesize were still hardcoded to AV_PIX_FMT_YUV420P
sizing despite colours/pixelFormat being set correctly from DB.
For RGBA monitors, shared memory slots were 3.7x too small, causing
buffer overflow and intermittent corruption in the live stream.

Use the Camera's pixelFormat member (set from p_colours/p_subpixelorder)
for linesize and imagesize computation.

refs #4735

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-18 21:30:47 -04:00
Isaac ConnorandClaude Opus 4.6 b416aa5293 fix: replace ZM_COLOUR_*/ZM_SUBPIX_ORDER_* with AVPixelFormat for format dispatch
ZM_COLOUR_GRAY8, ZM_COLOUR_YUV420P, and ZM_COLOUR_YUVJ420P were all
defined to 1, making format identification via colours ambiguous.
LocalCamera misidentified YUV420P as GRAY8, causing V4L2 MJPEG cameras
to decode to grayscale via expensive sws_scale conversion.

Replace the legacy ZM_COLOUR_*/ZM_SUBPIX_ORDER_* integer pair with
AVPixelFormat as the single source of truth for pixel format dispatch:

- Add src/zm_pixformat.h with central format helpers:
  zm_pixformat_from_colours, zm_colours_from_pixformat,
  zm_bytes_per_pixel, zm_db_colours_to_pixformat, zm_is_rgb32,
  zm_is_rgb24, zm_is_yuv420
- Add AVPixelFormat pixelFormat member + PixelFormat() accessor to Camera
- Add PixFormat() accessor to Image, delegate AVPixFormat methods
  to shared helpers
- Migrate all ~100 format dispatch comparisons in zm_image.cpp,
  zm_local_camera.cpp, zm_ffmpeg_camera.cpp, zm_remote_camera_rtsp.cpp,
  zm_libvlc_camera.cpp, zm_libvnc_camera.cpp, zm_monitor.cpp,
  zm_mpeg.cpp from colours/subpixelorder checks to imagePixFormat/
  AVPixelFormat checks
- Deprecate GetFFMPEGPixelFormat, delegate to zm_pixformat_from_colours
- Fix DeColourise bug: imagePixFormat was not updated to GRAY8
- Deprecate ZM_COLOUR_* and ZM_SUBPIX_ORDER_* constants in zm_rgb.h
- Add deprecation notice on Monitor.Colours web UI dropdown
- Add 13 Catch2 test cases (105 assertions) for format mapping helpers

refs #4735

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-18 11:42:06 -04:00
Isaac ConnorandClaude Opus 4.6 cf27243055 fix: add DTS backward jump detection in ffmpeg camera capture
The existing PTS backward jump check in FfmpegCamera::Capture() does not
catch cases where DTS jumps back significantly (e.g. stream restarts,
encoder resets) while PTS may remain unaffected. This causes the
VideoStore to spend minutes forcing DTS monotonicity on every packet via
the write_packet fixup path, flooding logs with warnings.

Add per-stream DTS tracking (mLastVideoDTS/mLastAudioDTS) and a backward
jump check mirroring the existing PTS check: if DTS jumps back more than
10 seconds, increment error_count and after 6 occurrences return -1 to
trigger capture reconnection.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-29 09:52:00 -04:00
Isaac ConnorandClaude Opus 4.5 df6c4a4a13 fix: address multiple bugs across core source files
- zm_analysis_thread.cpp, zm_decoder_thread.cpp: Fix potential deadlock
  in Start() by calling Stop() before joining thread
- zm_camera.cpp: Add null check after avformat_alloc_context()
- zm_comms.cpp: Fix memory leak in InetSocket::bind(), fix error message
  typo in deleteReader(), fix clearReaders/clearWriters to recalculate
  mMaxFd properly
- zm_config.cpp: Fix potential buffer underrun when parsing config files,
  fix misplaced SERVER_ID check logic
- zm_db.cpp: Fix logger level not restored on early return in zmDbDo(),
  fix empty string access in DB_HOST parsing
- zm_event.cpp: Fix typo "foudn" -> "found", fix memory leak with Tag
  allocation, fix variable shadowing with video_file
- zm_event_tag.cpp: Fix null dereference when AssignedBy is NULL
- zm_eventstream.cpp: Fix dangling pointer bugs with emplace_back
  (use auto& instead of auto), fix memory leak in loadInitialEventData

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-03 10:18:49 -05:00
Isaac ConnorandClaude Opus 4.5 dd4a055717 fix: move mVideoStreamId assignment inside null check
If avformat_new_stream() returns nullptr, the code would log an error
but then still dereference mVideoStream to get the index, causing a
null pointer dereference crash.

Move the assignment inside the if block where mVideoStream is known
to be valid.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 20:35:58 -05:00
Aaron Kling c4683d90a9 Format code using astyle google format
Commands used:
astyle --style=google --indent=spaces=2 --keep-one-line-blocks src/*.cpp
astyle --style=google --indent=spaces=2 --keep-one-line-blocks src/*.h
2024-03-26 13:43:58 -05:00
Isaac Connor db135cba46 Add isPrimed to camera class instead of it just being in ffmpeg_camera. Rename mCanCapture to isPrimed in ffmpeg_camera. 2023-12-06 09:38:38 -05:00
Isaac Connor 61e5527b63 Log image size as wel 2022-11-30 13:18:13 -05:00
Doug Nazar 74f8e43b12 Cast operand to larger destination size to avoid possible overflow issues. 2022-07-31 00:48:27 -04:00
Isaac Connor cecb8f65b7 Try out close_input instead of free_context 2022-06-08 23:27:51 -04:00
Peter Keresztes Schmidt 2d71743372 FFmpeg: Remove code paths required only by 2.8 and older
With Xenial support dropped we require FFmpeg 3.2 and newer.
2021-06-05 20:40:12 +02:00
Isaac Connor f077ec6145 Take first PTS into account when calculating last pts for figuring out which input to use. 2021-04-11 16:35:16 -04:00
Isaac Connor eaaf04420a Keep track of stream last_pts. So we can at least try to sync streams 2021-03-03 12:45:05 -05:00
Isaac Connor b87d859f72 Set the packet's stream_index to the packetqueue stream. Rename get_ functions to get 2021-03-03 12:06:34 -05:00
Isaac Connor 86541779d9 Add SecondFormatContext 2021-03-03 09:55:57 -05:00
Peter Keresztes Schmidt f43507dce0 Use the power of smart pointers to manage Monitor instances 2021-02-07 21:20:45 +01:00
Isaac Connor 513739aeb5 Merge pull request #3127 from Carbenium/header-cleanup
Cleanup and reorganize includes
2021-02-04 12:52:04 -05:00
Peter Keresztes Schmidt 0dbc39ee25 Cleanup and reorganize includes
With this commit a unified structure for includes is introduced.
The general rules:
 * Only include what you need
 * Include wherever possible in the cpp and forward-declare in the header

 The includes are sorted in a local to global fashion. This means for the include order:
  0. If cpp file: The corresponding h file and an empty line
  1. Includes from the project sorted alphabetically
  2. System/library includes sorted alphabetically
  3. Conditional includes
2021-02-04 18:02:01 +01:00
Isaac Connor ad9fd05955 When allocating mVideoStream, set mVideoStreamId as well 2021-02-04 10:28:17 -05:00
Isaac Connor 28d27cc30a use camel case on video_stream and audio_stream 2021-01-28 10:45:19 -05:00
Isaac Connor 4c933f82b8 All cameras will have an mFormatConext, CodecContext etc. Consolidate code in zm_camera. Fix remote rtsp decoding 2021-01-27 16:12:32 -05:00
Isaac Connor 66c78e5a9b Merge branch 'zma_to_thread' into rtsp_server 2021-01-19 13:43:25 -05:00
Isaac Connor a39a656373 Merge branch 'master' into zma_to_thread 2020-09-29 11:02:40 -04:00
Peter Keresztes Schmidt 8f980a1168 Convert NULL/0 to nullptr
The 0 -> nullptr changes should definitely improve readability.
2020-08-26 22:03:40 +02:00
Isaac Connor 869860bb26 Merge branch 'master' into zma_to_thread 2020-07-21 17:49:39 -04:00
Isaac Connor 864cd4c178 introduce a linesize variable which represents the # of bytes to hold a line of the image in the buffer. May be > width * colours 2020-07-05 17:51:57 -04:00
Isaac Connor 279e0d8bcf Merge branch 'storageareas' into zma_to_thread 2019-02-22 11:39:58 -05:00
Isaac Connor 443fd35d99 Fix imagesize requirements (#2404)
* introduce non-loop-unrolled version of function and use them when the image size is not a multiple of 12 or 16

* Remove tests for imagesize being a multiple of 16 or 12 to handle functions with unrolled loops

* Use non-unrolled functions when image size is not a multiple of 12 or 16

* use std_blend when image is odd size
2019-02-22 09:44:57 -05:00
Isaac Connor d3c95ea144 fix merges 2018-05-12 19:44:20 -04:00
Isaac Connor c3053a2f8a Merge branch 'storageareas' into zma_to_thread 2018-05-10 17:51:38 -04:00
Isaac Connor 4d4a7a4221 Initialize bytes to zero in camera constructor. 2018-05-07 10:27:06 -04:00
Isaac Connor bc525a4e01 blah 2017-12-02 13:55:26 -05:00
Isaac Connor 74650ebc70 simplify Analysis 2017-11-20 11:48:56 -05:00
Isaac Connor 22ff831859 more cppcheck recommendations 2017-11-16 21:21:56 -05:00
Isaac Connor fd44ff4e60 Merge branch 'master' into storageareas 2017-05-10 13:16:08 -04:00
Kfir Itzhak 71e6735b60 Neon32 functions now work on 64 bytes at a time. This results in 4-6x performance increase over standard functions
Memory allocations and image size requirements changed to be as needed for 64 byte alignment.
Self-test code for Blend modified accordingly and added Self-test for the delta functions.
2017-04-16 10:57:37 +03:00
Isaac Connor bdb6dd63a3 change type of width and height to unsigned int and recording from bool to timeval 2017-04-12 13:32:22 -04:00
Isaac Connor 5ae34a7561 Merge branch 'master' into storageareas 2017-01-02 09:39:10 -05:00
Andy Bauer 254fcbcef7 update gpl 2 mailing address in source files 2016-12-26 09:23:16 -06:00
Isaac Connor 3a06ba7a65 Turn off instantiating a default zone before even loading the zones. Implement the use of Camera->setMonitor so that the camera object doesn't have to load it later. 2016-09-09 09:15:04 -04:00
Isaac Connor 62a2ce56fb Merge Rotation/Orientation work, rename id to monitor_id in cameras and also make it an unsigned int since logically that is what it is. Also some fixups for bad rtsp h264 streams. 2016-08-10 12:19:53 -04:00
Isaac Connor e29a0ebe71 Merge branch 'feature-h264-videostorage' into storageareas 2016-06-21 13:48:32 -04:00
Isaac Connor 6a8db582ff whitespacing updates 2016-06-21 12:21:18 -04:00
Isaac Connor 71e9553648 add rotation to video 2016-05-13 14:51:26 -04:00
Steve Gilvarry da157f5a1a Merge remote-tracking branch 'upstream/master' into feature-h264-videostorage 2016-04-30 22:27:10 +10:00