Skip to content

Conversation

cedricve
Copy link
Member

@cedricve cedricve commented Jul 13, 2025

Description

Pull Request: feature/optimise-memory-management-decoder

Motivation and Improvement

This pull request aims to optimize memory management within the H264/H265 video decoder, enhancing performance and reliability. The changes are driven by the need to improve decoding speed and ensure better handling of memory in the Go code interfacing with C libraries.

Key Changes

  1. Include Additional Libraries:

    • Added stdlib.h and string.h for memory management functions.
    • Included libavutil/hwcontext.h to support hardware acceleration contexts.
  2. Optimize Decoder for Speed:

    • Configured the decoder to utilize multiple threads by setting the thread count to the number of available CPU cores. This change leverages multi-threading, significantly improving decoding performance on multi-core systems.
  3. Memory Management Improvements:

    • Replaced manual memory allocation (C.CBytes) and deallocation (C.free) with runtime.Pinner to pin memory during CGO calls. This prevents garbage collection from moving memory during the operation, improving stability and preventing potential memory errors.
    • Simplified the process of prepending the NALU start code by using helper functions and creating new slices. This avoids issues with CGO pointers and ensures safe memory handling.
  4. Code Cleanup and Refactoring:

    • Removed the fromCPtr function and replaced its usage with the unsafe.Slice function for better readability and performance.
    • Fixed minor inconsistencies in error messages and comments for clarity and consistency.

Benefits to the Project

  • Enhanced Performance: Multi-threading optimizations will lead to faster decoding times, making the application more responsive and efficient.
  • Improved Stability: Better memory management techniques reduce the likelihood of memory-related errors, enhancing the reliability of the decoder.
  • Code Maintainability: Refactoring and code cleanup make the codebase easier to understand and maintain, facilitating future contributions and debugging.

Overall, these changes contribute to a more robust and efficient video decoding process, aligning with the project's goals of delivering high-performance and reliable software.

@Copilot Copilot AI review requested due to automatic review settings July 13, 2025 08:05
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR optimizes memory management in the video decoder by replacing C.CBytes allocations with Go’s runtime.Pinner and unsafe.Slice, adds multithreading for decoding, and cleans up helper functions/imports.

  • Switch from reflect based slice headers to unsafe.Slice and use runtime.Pinner for cgo buffer pinning
  • Add a hardcoded thread count to the decoder context for parallel decoding
  • Update includes, error message casing, and remove the fromCPtr helper
Comments suppressed due to low confidence (3)

machinery/src/capture/gortsplib.go:1056

  • The code uses unsafe.Pointer and unsafe.Slice but there is no import for the unsafe package; add import "unsafe" to ensure it compiles.
	avPacket.data = (*C.uint8_t)(unsafe.Pointer(&naluWithStartCode[0]))

machinery/src/capture/gortsplib.go:899

  • The error message was changed from "Empty image" to "empty image"; this casing change could break consumers that match on the exact message. Verify that this lowercasing is intended.
		return image.YCbCr{}, errors.New("empty image")

machinery/src/capture/gortsplib.go:5

  • [nitpick] The <string.h> header is included but none of its functions are used; removing it will keep the C includes minimal and avoid confusion.
// #include <string.h>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant