0

I have the following function that processes a buffer object containing a video frame supplied by GStreamer

def __handle_videoframe(self, appsink):
    """
    Callback method for handling a video frame

    Arguments:
    appsink -- the sink to which gst supplies the frame (not used)
    """ 

    buffer = self._videosink.emit('pull-buffer')    
    (w,h) = buffer.get_caps[0]["width"],buffer.get_caps[0]["height"]

    reqBufferLength = w * h * 3 #Required buffer lenght for a raw rgb image of these dimensions     
    print "Buffer length: " + str(len(buffer.data))
    print "Needed length: " + str(reqBufferLength)          

    img = pygame.image.frombuffer(buffer.data, self.vidsize, "RGB")     
    self.screen.blit(img, self.vidPos)      
    pygame.display.flip()

When running this code however, pygame crashes because the supplied buffer is larger than required and this size needs to match. I know this is probably caused by a faulty encoding of the movie that is played (as most movies do run fine), but is there a way to account for this contingency? Is there a way to resize the buffer on the go to a correct size? I have tried to just cut-off the tail of the buffer at the required length and then the movie does play, but the output is corrupted.

Daniel Schreij
  • 773
  • 1
  • 10
  • 26

1 Answers1

0

ok, a better solution was to use bufferproxies. They are less fuzzy about the length of the buffer.

img_sfc = pygame.Surface(video_dimensions, pygame.SWSURFACE, 24, (255, 65280, 16711680, 0))
img_buffer = img_sfc.get_buffer()

Then for each new frame:

img_buffer.write(buffer.data, 0)
pygame.display.get_surface().blit(img_sfc.copy(), vid_pos)

And voila, even incorrectly formatted buffers appear on screen without problems

Daniel Schreij
  • 773
  • 1
  • 10
  • 26