NVIDIA / VideoProcessingFramework

Set of Python bindings to C++ libraries which provides full HW acceleration for video decoding, encoding and GPU-accelerated color space and pixel format conversions
Apache License 2.0
1.31k stars 233 forks source link

surface.Crop Error: Segmentation fault #489

Open AlexanderSlav opened 1 year ago

AlexanderSlav commented 1 year ago

Describe the bug While trying to use the surface.Crop function in docker container (not original one), I encountered a segmentation fault error. The program crashes when trying to crop a decoded surface. In original docker container, the function call results in the program freezing indefinitely.

To Reproduce

Run the following code snippet that reproduces the error:

import PyNvCodec as nvc

def main():
    video_path = "rtmp://video.osai.ai/vod/big_buck_bunny.mp4"

    decoder = nvc.PyNvDecoder(input=video_path, gpu_id=0)
    surface = decoder.DecodeSingleSurface()

    if surface.Empty():
        print("Can't decode frame. End of frames.")
        return

    print("Decoded surface:", surface)

    crop_coords = (0.0, 0.1, 0.5, 0.8)

    crop_coords_abs = (
        int(crop_coords[0] * surface.Width()),
        int(crop_coords[1] * surface.Height()),
        int(crop_coords[2] * surface.Width()),
        int(crop_coords[3] * surface.Height()),
    )

    print("Calculated coordinates:", crop_coords_abs)
    print("Video HxW",  surface.Height(), surface.Width())
    print("Trying to make a crop....")
    cropped_surface = surface.Crop(
        x=crop_coords_abs[0],
        y=crop_coords_abs[1],
        w=crop_coords_abs[2] - crop_coords_abs[0],
        h=crop_coords_abs[3] - crop_coords_abs[1],
        gpu_id=0,
    )
    print("Cropping finished....")

    print("Cropped surface:", cropped_surface)

if __name__ == "__main__":
    main()

in my work dokai container i got following log:

Decoded surface: Width: 1280
Height:           720
Format:           NV12
Pitch:            1536
Elem size(bytes): 1
Plane 0
  Owns mem:  0
  Width:     1280
  Height:    1080
  Pitch:     1536
  Elem size: 1
  Cuda ctx:  0x564276ca8990
  CUDA ptr:  77594624

Calculated coordinates: (0, 72, 640, 576)
Video HxW 720 1280
Trying to make a crop....
Segmentation fault (core dumped)

Expected behavior The surface.Crop function is expected to create and return a new surface that is a cropped version of the original decoded surface without causing a crash or freeze.

Desktop

Additional context I initially encountered this problem in a container based on the dokai image - https://github.com/osai-ai/dokai/tree/master#vpf. I also attempted to reproduce the error in the original VPF docker container. While I couldn't fully reproduce the segmentation fault, the call to surface.Crop resulted in the program freezing indefinitely with following log:

Decoded surface: Width: 1280
Height:           720
Format:           NV12
Pitch:            1536
Elem size(bytes): 1
Plane 0
  Owns mem:  0
  Width:     1280
  Height:    1080
  Pitch:     1536
  Elem size: 1
  Cuda ctx:  0x556699893600
  CUDA ptr:  54525952

Calculated coordinates: (0, 72, 640, 576)
Video HxW 720 1280
Trying to make a crop....
AlexanderSlav commented 1 year ago

Hi,@RomanArzumanyan, what is your thoughts ?

RomanArzumanyan commented 1 year ago

Hi @AlexanderSlav

I'm now supporting VPF as community member so less bandwidth to investigate and fix. Following the general approach I'd ask if the problem is specific to Docker environment or reproduces on bare metal as well?

@theHamsta @gedoensmax Could you please take a look?

P. S. Extending the API test coverage would also be helpful: https://github.com/NVIDIA/VideoProcessingFramework/blob/master/tests/test_PySurface.py

AlexanderSlav commented 1 year ago

Thanks for reply ! Did not try it on bare metal due to our way of work politic. We are not installing anything directly on host machine Will check the API coverage test, thanks !

qiaoxuekang commented 8 months ago

So, how to crop surface?

RomanArzumanyan commented 8 months ago

Hi @qiaoxuekang

Please consider checking out https://github.com/RomanArzumanyan/VALI, it's a VPF successor which is actively developed and maintained. It has compatible API and module naming.

If this issue reproduces on latest VALI version, please submit a bug.