+static int copy_cvpixelbuffer(AVFormatContext *s,
+ CVPixelBufferRef image_buffer,
+ AVPacket *pkt)
+{
+ AVFContext *ctx = s->priv_data;
+ int src_linesize[4];
+ const uint8_t *src_data[4];
+ int width = CVPixelBufferGetWidth(image_buffer);
+ int height = CVPixelBufferGetHeight(image_buffer);
+ int status;
+
+ memset(src_linesize, 0, sizeof(src_linesize));
+ memset(src_data, 0, sizeof(src_data));
+
+ status = CVPixelBufferLockBaseAddress(image_buffer, 0);
+ if (status != kCVReturnSuccess) {
+ av_log(s, AV_LOG_ERROR, "Could not lock base address: %d (%dx%d)\n", status, width, height);
+ return AVERROR_EXTERNAL;
+ }
+
+ if (CVPixelBufferIsPlanar(image_buffer)) {
+ size_t plane_count = CVPixelBufferGetPlaneCount(image_buffer);
+ int i;
+ for(i = 0; i < plane_count; i++){
+ src_linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(image_buffer, i);
+ src_data[i] = CVPixelBufferGetBaseAddressOfPlane(image_buffer, i);
+ }
+ } else {
+ src_linesize[0] = CVPixelBufferGetBytesPerRow(image_buffer);
+ src_data[0] = CVPixelBufferGetBaseAddress(image_buffer);
+ }
+
+ status = av_image_copy_to_buffer(pkt->data, pkt->size,
+ src_data, src_linesize,
+ ctx->pixel_format, width, height, 1);
+
+
+
+ CVPixelBufferUnlockBaseAddress(image_buffer, 0);
+
+ return status;
+}
+