0

In my application I need to

  1. Create 24 CVPixelBufferRef
  2. Add them later to AVAssetWriterInputPixelBufferAdaptor in a custom order to write an mp4 movie.

The VideoExport::addFrame function receives raw pixel data and stores it in the next empty CVPixelBufferRef. Here is the demo code:

// .h
CVPixelBufferRef buffers[24];

// .mm
void VideoExport::addFrame(unsigned char * pixels, long frame_index) {
    buffers[frame_index] = NULL;
    CVPixelBufferCreateWithBytes(NULL,
                            frameSize.width,
                            frameSize.height,
                            kCVPixelFormatType_24RGB,
                            (void*)pixels,
                            frameSize.width * 3,
                            NULL,
                            0,
                            NULL,
                            &buffers[frame_index]);
}

The pixel buffers seem to populate successfully. The problem is that when I try writing different frames to the movie file by changing index in buffers[index], the same frame is saved, over an over again.

The frame that gets saved seems to always be the last one I sent to addFrame, defeating my attempt of using an array of unique buffers. I suspect that any call to addFrame overwrites the previous data.

Note 1: I have tested that the pixels sent to addFrame are unique.

Note 2: If I add the frame to the movie immediately inside addFrame the produced movie has unique frames, but then I can't shuffle the frame order.

What would be the correct way to create and reuse an array of CVPixelBufferRef? Would a pixelBufferPool help, and if so, how can I use it?

Thank you.

Thinium
  • 171
  • 1
  • 14

1 Answers1

0

OK, found the issue.

In my addFrame function, the unsigned char * pixels was destroyed after every addFrame call. But to make the buffers array usable later, the pixels need to remain alive until the buffers array is destroyed.

Thinium
  • 171
  • 1
  • 14