How to List all OpenGL ES Compatible PixelBuffer Formats

1.1k views Asked by At

Is there a way to list all CVPixelBuffer formats for CVPixelBufferCreate() that will not generate error -6683: kCVReturnPixelBufferNotOpenGLCompatible when used with CVOpenGLESTextureCacheCreateTextureFromImage()?

This lists all the supported CVPixelBuffer formats for CVPixelBufferCreate(), but does not guarantee that CVOpenGLESTextureCacheCreateTextureFromImage() will not return the error above. I guess my desired list should be a subset of this one.

2

There are 2 answers

0
Stéphane Copin On BEST ANSWER

Based on the answer from Adi Shavit above, here's a full code snippet (Objective-C) that you can use to print all the current OpenGL ES compatible pixel formats:

+ (void)load {
  @autoreleasepool {
    printf("Core Video Supported Pixel Format Types:\n");

    CFArrayRef pixelFormatDescriptionsArray = CVPixelFormatDescriptionArrayCreateWithAllPixelFormatTypes(kCFAllocatorDefault);
    for (CFIndex i = 0; i < CFArrayGetCount(pixelFormatDescriptionsArray); i++) {
      CFNumberRef pixelFormatFourCC = (CFNumberRef)CFArrayGetValueAtIndex(pixelFormatDescriptionsArray, i);

      if (pixelFormatFourCC != NULL) {
        UInt32 value;

        CFNumberGetValue(pixelFormatFourCC, kCFNumberSInt32Type, &value);

        NSString * pixelFormat;
        if (value <= 0x28) {
          pixelFormat = [NSString stringWithFormat:@"Core Video Pixel Format Type 0x%02x", (unsigned int)value];
        } else {
          pixelFormat = [NSString stringWithFormat:@"Core Video Pixel Format Type (FourCC) %c%c%c%c", (char)(value >> 24), (char)(value >> 16), (char)(value >> 8), (char)value];
        }

        CFDictionaryRef desc =  CVPixelFormatDescriptionCreateWithPixelFormatType(kCFAllocatorDefault, (OSType)value);
        CFBooleanRef OpenGLESCompatibility = (CFBooleanRef)CFDictionaryGetValue(desc, kCVPixelFormatOpenGLESCompatibility);
        printf("%s: Compatible with OpenGL ES: %s\n", pixelFormat.UTF8String, (OpenGLESCompatibility != nil && CFBooleanGetValue(OpenGLESCompatibility)) ? "YES" : "NO");
      }
    }

    printf("End Core Video Supported Pixel Format Types.\n");
  }
}

You can put this snippet anywhere in a Objective-C category or class to print which pixel format are compatible and which aren't. For completeness purposes, here are all the pixel formats currently compatible with OpenGL ES as of iOS 10.2.1:

  • L565
  • 5551
  • L555
  • 2vuy
  • yuvs
  • yuvf
  • L008
  • L016
  • 2C08
  • 2C16
  • BGRA
  • 420v
  • 420f
  • 420e
  • 411v
  • 411f
  • 422v
  • 422f
  • 444v
  • 444f
  • L00h
  • L00f
  • 2C0h
  • 2C0f
  • RGhA
  • RGfA
0
Adi Shavit On

Answering myself... after further study of this link.
The function CVPixelFormatDescriptionArrayCreateWithAllPixelFormatTypes() provides all the supported formats on a particular device (and OS). Once the fourCC value is found as shown in the link, you can use CVPixelFormatDescriptionCreateWithPixelFormatType() to get a dictionary with various fields describing each format.

This looks something like this:

UInt32 value;
CFNumberGetValue(pixelFormatFourCC, kCFNumberSInt32Type, &value);
auto desc =  CVPixelFormatDescriptionCreateWithPixelFormatType(kCFAllocatorDefault, (OSType)value);
auto OpenGLCompatibility = (CFBooleanRef)CFDictionaryGetValue(desc, kCVPixelFormatOpenGLCompatibility);
auto OpenGLESCompatibility = (CFBooleanRef)CFDictionaryGetValue(desc, kCVPixelFormatOpenGLESCompatibility);

If OpenGLESCompatibility is true then this format is OpenGL ES compatible.