Commit Graph
202 Commits
Author SHA1 Message Date
Jash 61b700a522 fix: map YUVJ411P to YUV411P in fix_deprecated_pix_fmt refs #6592 2026-11-17 20:46:08 -05:00
Jash 9440f4cc82 fix: reset swscale range to limited for plain YUV after a YUVJ conversion on a cached context refs #6457 2026-11-16 17:12:31 +05:30
singhharsh1708 4f9658e22d fix: treat # as a comment in encoder parameters again 2026-11-16 02:36:51 +05:30
Jash be8612f0fb fix: fall back to the directory cmake found libavdevice in when dlopen by name fails refs #5526 2026-11-15 11:42:42 -05:00
Jash 52f8b698b6 perf: load libavdevice only when an ffmpeg V4L2 input needs it fixes #5526 2026-11-15 11:42:42 -05:00
Isaac Connor 6a30601145 Merge pull request #5667 from AJ0070/decoder-thread-count
fix: ignore an invalid thread_count decoder option instead of aborting
2026-11-14 23:58:50 -05:00
Isaac Connor a03b9cc2a0 Merge pull request #5669 from AJ0070/hwaccel-profile-check
fix: let FFmpeg reject hardware decoding of unsupported profiles
2026-11-14 23:57:20 -05:00
Isaac Connor 84bbe0dd49 Merge pull request #5668 from AJ0070/hwaccel-format-fallback
fix: fall back to software decoding when the hardware surface format isn't offered
2026-11-14 23:57:02 -05:00
Jash d7fda32484 fix: add the av1_nvenc and vp9_qsv encoders the monitor form offers to the encoder table refs #5928 2026-11-15 09:47:15 +05:30
Jash 60889836a5 fix: ignore an invalid thread_count decoder option instead of aborting fixes #5664 2026-11-15 09:17:12 +05:30
Jash f7cb398809 fix: don't abort zmc on a non-numeric bitrate, gop_size or reorder_queue_size encoder option refs #5922 2026-11-14 23:20:32 +05:30
Jash 405272c6be fix: skip encoder parameter entries without a value instead of dropping the next option refs #5922 2026-11-14 23:20:32 +05:30
Jash 7f937c4f76 fix: let FFmpeg reject hardware decoding of unsupported profiles fixes #5666 2026-11-13 12:26:45 +05:30
Jash 4509de788c fix: fall back to software decoding when the hardware surface format isn't offered fixes #5665 2026-11-13 12:26:43 +05:30
Jash 8a5a75b587 fix: drop input codec tags the output muxer rejects when copying video fixes #5491 2026-11-08 20:23:58 +05:30
Isaac ConnorandClaude Opus 4.8 7b2d8fc49d fix: reuse one encoder hw device context per monitor instead of per event refs #5300
VideoStore created and destroyed a VAAPI hardware device context for every
event, opening /dev/dri/renderD128 on open and closing it on close. On some
VAAPI drivers the teardown did not actually close the render-node fd, so a
continuously recording monitor leaked one fd per event (one per section, about
every ten minutes for the reporter). After ~1024 fds the process hit its
open-file limit and VideoStore could no longer open, falling back to jpegs.

Cache the device context on the Monitor and hand VideoStore a borrowed
reference through Monitor::GetEncoderHWDeviceContext(). The render node is now
opened once for the monitor's lifetime and reused by every event, so there is
no per-event device teardown to leak, and encoder setup is cheaper. The
contexts are released in ~Monitor once no VideoStore can be borrowing them.

The cache keys one context per (hwdevice type, device) and never evicts,
because the auto encoder list interleaves VAAPI and QSV: a single cached slot
would be dropped when a failed probe of the other type ran between two probes
of the type actually used, recreating the device every event and preserving
the leak. Keying per type keeps each device for the monitor's life regardless
of probe order.

setup_hwaccel() now reuses a device context the caller supplies and only
creates its own when none is given, so the decoder path and the fallback (when
the monitor cannot provide one) are unchanged.

Validated with a standalone reproduction against VAAPI (ffmpeg 9, Intel iHD):
creating and freeing a device per iteration keeps the renderD128 fd count flat
on this driver, while the shared-device model holds it at one for the lifetime
regardless. A Catch2 test is not added because it requires a GPU and CI runs
with the test suite disabled.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-22 17:03:38 -04:00
Isaac ConnorandClaude Opus 5 ef629bdeaf refactor: move the video decoder open out of FfmpegCamera
Second piece of groundwork for letting the decoder thread own decoding. The
whole decoder setup -- codec selection, context allocation, thread count,
option defaults and overrides, hwaccel negotiation, avcodec_open2 and the
fallback -- sat as 184 lines inside FfmpegCamera::OpenFfmpeg(), reachable only
by a camera. Monitor cannot call it there, so the move would have meant
copying it.

Lift it to open_video_decoder() in zm_ffmpeg.cpp, leaving the camera a call
and the null check. No behaviour change: the body is the same code with camera
members replaced by parameters.

hw_pix_fmt and hw_device_ctx are taken by reference and keep their names, which
is not only convenience. The returned context stores &hw_pix_fmt in ctx->opaque
and get_hw_format dereferences it on every format negotiation, so the storage
has to outlive the context. A reference parameter says that at the signature
and makes a temporary impossible; the header says why.

Two things the extraction had to account for:

  opts was declared once for the whole of OpenFfmpeg and shared with the input
  open. It is freed unconditionally before the decoder block and nothing
  repopulates it in between, so a fresh local is equivalent. The
  AVDictionaryEntry it scanned with was shared the same way and is now local
  too.

  use_hwaccel was a camera member but only ever read at the gate inside this
  block, cleared when a candidate's hwaccel failed so the next codec attempt
  would not retry it. That is entirely internal to the open, so it is a local
  now and the member is gone rather than left looking like it still gates
  something.

Verified on hardware rather than by reading: an rtsp hevc monitor with
DecoderHWAccelName=vaapi still negotiates ("Decoder hevc supports type vaapi",
"Using vaapi hardware decoding for hevc", now logged from zm_ffmpeg.cpp) and
decoded 749 frames into a recorded event with no errors.

Build clean with no new warnings; suite 147 cases / 12531 assertions.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015Y6FieTwEXuLhhR4e2yiax
2026-10-10 09:32:17 -04:00
Isaac Connor d376003495 refactor: extract which hwaccel types a DecoderHWAccelName asks to try
Groundwork for moving decoding out of the camera and into the decoder thread.
The parsing of DecoderHWAccelName sat inline in FfmpegCamera::OpenFfmpeg(),
where nothing could test it, and the ai_server branch has a second copy of the
same logic in its Monitor-side decoder open. Moving the decoder without pulling
this out first would carry the duplication across rather than end it.

No behaviour change: same iteration over the types this build offers, same
comma-separated priority list, same warn-and-skip for a name libavutil does not
know, same order.

Being a free function it can now be tested, which it could not be before. The
cases covered are the ones an operator's setting actually runs into -- a single
name, a priority list, spaces around the commas, a typo among good entries,
empty entries -- asserted against whatever device types the host reports rather
than against a fixed list, so the test travels. This host offers seven, so no
section skipped for want of data.

Confirmed non-vacuous by mutation: dropping TrimSpaces fails one case, and
accepting an unknown name instead of skipping it fails two.

Build clean with no warnings; suite 147 cases / 12531 assertions.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015Y6FieTwEXuLhhR4e2yiax
2026-10-10 09:32:17 -04:00
Isaac ConnorandClaude Opus 5 a5cd44df35 fix: map deprecated YUVJ pixel formats on the swscale output side fixes #5037
A monitor whose source signals full range (Dahua h264) decodes to
AV_PIX_FMT_YUVJ420P, and Monitor passes that format through to shared memory
unchanged. When zms scales such a frame for montage or console thumbnails,
SWScale::Convert mapped only the input format through fix_deprecated_pix_fmt()
and handed the still-deprecated YUVJ format to swscale as the destination, so
libswscale logged

  deprecated pixel format used, make sure you did set range correctly

for every context it built. Watch view at 100% scale never hit it because
Image::Scale returns early when the dimensions already match.

Map deprecated formats on both sides in SWScale::Convert (both the buffer and
the AVFrame overload), in Image::Assign(AVFrame*), in
Monitor::setupConvertContext and in the LocalCamera conversion context.

Mapping the destination YUVJ format to its non-J equivalent makes swscale
default that side to limited range, which would compress full-range output.
Replace zm_sws_set_input_range() with zm_sws_set_ranges(), which takes the
original pre-fix formats for both sides and sets srcRange/dstRange accordingly.

Image::Assign(AVFrame*) now compares the mapped source and destination formats,
so a YUVJ420P frame into a YUVJ420P image takes the av_image_copy fast path
instead of running through swscale.

Image::Scale built a fresh SWScale, and therefore a fresh sws context, on every
call. That is a full sws_init_context per scaled frame per stream, and it is
what turned the warning into a per-frame flood rather than a one-off. Reuse a
thread_local SWScale; sws_getCachedContext re-inits itself when the geometry
changes.

Tests: zm_swscale_range.cpp installs an av_log callback and asserts that
SWScale::Convert with a YUVJ destination and Image::Scale on a YUVJ420P image
emit no deprecated-format message, plus a luma check that the range handling
still survives the mapping.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NDhTBPj9xEaT52pRmAufaP
2026-08-05 17:44:37 -04:00
Isaac ConnorandClaude Opus 4.8 74bf0637f0 fix: restore generic decoder fallback for ffmpeg cameras
Extract the "use whatever decoder ffmpeg provides" logic from
FFmpeg_Input into a shared open_fallback_decoder() and call it from
FfmpegCamera, which had lost the fallback: its generic path only ran
inside the preferred-codec loop, so an empty get_decoder_data() result
failed outright instead of trying the codec's default decoder.

The dec_codecs table stays a preference list; a codec present in the
ffmpeg build but absent from the table (e.g. libopenh264 for H264) now
decodes via the fallback instead of requiring a hard-coded entry.

Add tests/zm_ffmpeg_fallback.cpp covering the fallback with a codec not
in dec_codecs (mpeg2video), the optional codec_out argument, and the
no-decoder case.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-19 11:26:03 -04:00
Isaac ConnorandClaude Opus 4.8 346a4baa82 feat: preserve full-range colour when converting YUVJ frames via swscale
swscale assumes limited (MPEG, 16-235) input range by default. Mapping a
full-range JPEG format (YUVJ*) to its non-J equivalent for the conversion
therefore washes the colours out, because full-range luma/chroma gets
treated as limited.

Add pix_fmt_is_jpeg_range() and zm_sws_set_input_range(), which sets
srcRange=1 on the context when the original source format was full range.
Apply it at every conversion site that takes decoded frames: Image::Assign
and both SWScale::Convert overloads. Monitor::setupConvertContext already
did this inline with a hand-rolled switch and colorspace block; replace
that with the shared helpers to remove the duplication.

Add tests/zm_swscale_range.cpp: converting a Y=16 YUVJ420P image to RGB24
keeps luma ~16 (full range) rather than being crushed to ~0 (limited).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-08 19:21:25 -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 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 Connor 24e16796b7 Add mpeg4 as some older cameras that use are still funcitoning 2026-02-05 09:08:02 -05:00
Isaac ConnorandClaude Opus 4.5 0217cfbab0 fix: correct misleading debug messages in zm_ffmpeg.cpp
Debug messages incorrectly used "!=" when describing matching conditions.
Changed to "==" to accurately reflect that the codec name/id matched.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-03 10:21:04 -05:00
Isaac Connor 2eebda8c64 Cleanup hw_pix_fmt and get_hw_format 2026-01-26 14:40:10 -05:00
Isaac Connor edb09314b5 The correct codec name is hevc not h265 2026-01-26 11:56:12 -05:00
Isaac Connor 0a0e29c99b Add get_fps_string to return a string of the fps content to be used later for Debug output. Convert dump_stream to build a string first before Debug 2026-01-26 11:47:21 -05:00
Isaac Connor d604dd69b6 Implement libjpeg_to_ffmpeg_qv 2026-01-26 11:46:47 -05:00
Isaac Connor fd7e2e7935 Fixup sw_pix_fmt options for hevc_vaapi and hevc_cuvid. Add h264_cuvid as an option. 2026-01-25 18:00:28 -05:00
Isaac Connor 30d0544613 Add better debugging of codec_tag in zm_dump_codec and zm_dump_codecpar 2025-10-27 15:04:19 -04:00
Isaac Connor a775d671d0 Move hwaccel decoders to bottom so that we don't get a bunch of failures logged trying to open hwaccel devices 2025-09-23 09:37:21 -04:00
Isaac Connor fbea774eda Fix h265->hevc codec 2025-09-18 14:24:48 -04:00
Isaac Connor 523ee9daf5 Finish change of codec_id to codec_name 2025-09-18 13:40:54 -04:00
Isaac Connor f9204e2d7a Add default options for codecs 2025-09-18 10:25:33 -04:00
Isaac Connor e05a9252ab Finish up zm_dump_stream(AVSstream) 2025-09-18 09:58:10 -04:00
Isaac Connor 24246106a9 Add software h265 codecs 2025-09-18 09:57:40 -04:00
Isaac Connor 40f2f5a27a Deal with ffmpeg 7.1 deprecations 2025-09-18 09:56:35 -04:00
Isaac Connor 5ff08270ec Fix name of h264_v4l2m2m codec. Was h264_v4lm2m. Reorder codecs based on hwaccel 2025-09-18 09:55:58 -04:00
Isaac Connor fa66d2baf4 Break up long line 2025-09-18 09:52:17 -04:00
Isaac Connor e1898c483b Adjust debug level add AVPixelFormat Image::AVPixFormat(AVPixelFormat new_pixelformat) 2025-09-18 09:42:03 -04:00
Isaac Connor c698b611ed Use HAVE_QUADRA instead of QUADRA. Add software h264 decoding 2025-09-18 09:39:54 -04:00
Isaac Connor 1063efbbfa Add handling of c++20 2025-09-18 09:36:54 -04:00
Isaac Connor 7709d9bb7b Move more stuff into zm_ffmpeg from zm_videostore 2025-09-18 09:36:18 -04:00
Isaac Connor e6c3d81f21 Disable avframes stuff 2025-09-18 09:34:09 -04:00
Isaac Connor 1edeaacd9d Add mjpeg decoders, remove Error logging from send_packet_receive_frame 2025-09-18 09:32:20 -04:00
Isaac Connor 4362770672 Remove deprecated avcodec_close 2025-09-18 09:31:13 -04:00
Isaac Connor 25a1c88686 Move codec_data from videostore to ffmpeg. Add dec_codecs and add a function to return relevant codec_data and setup hw_ctx. 2025-09-18 09:27:31 -04:00
Isaac Connor 55982ca423 Log qcompress as well 2025-09-18 09:22:52 -04:00