avassetreader convert hdr frames to proper colorized CIImages

there are a lot of examples on how to add HDR video support to your apps, but we want to do is read video frames from an HDR video file and then properly map the colors to CIImages so they don't look washed out. it seems like there is some sort of color transform that needs to be applied. but does anyone know the magic formula?

CVPixelBufferRef inputPixelBuffer = CMSampleBufferGetImageBuffer(buffer);

CIImage* ciImage = [CIImage imageWithCVPixelBuffer:inputPixelBuffer]; // one vid frame

  • seems like we are looking for some sort of HDR to SDR tonemapping CIFilter or perhaps a CGColorSpace that does this mapping

Add a Comment

Replies

You might want to add CoreImage or CoreGraphics tags to the question

  • we have prepped examples of the color/brightness problem with image extraction from HDR vids, and a code sample (a fully running xcode project app that runs on iOS simulator) of how we are doing it. alas we cannot edit the tags on the original post per note from above apple engineer. or, perhaps, we do not know how to do this.

    trying to get into the new apple tech "office hours" to discuss this, but that is a dice-roll

    https://www.dropbox.com/sh/dquc22xqkrxbt91/AADlFR7TrAk1s7K32kmkpeVVa?dl=0

Add a Comment

FYI we got a half hour "office hours" appointment and after some chatter and trial and error, this method seemed to do the trick at extracting images from HDR / Dolby vids with better color. We don't know if it is the full answer yet, but it is definitely better.

        // prior to here, set up the AVAsset * asset 

       // code is a bit of archaic ObjC syntax but it works

        NSArray* video_tracks = [asset tracksWithMediaType:AVMediaTypeVideo];

        AVAssetTrack* video_track = [video_tracks firstObject];

        NSMutableDictionary* dictionary = [[NSMutableDictionary alloc] init];

        // kCVPixelFormatType_32BGRA (pixel format we originally used)
        // 420YpCbCr8BiPlanarFullRange (new pixel format that seems to work)

        // need to change the pixel format to this new one here, and also set the video color prop key below (which is 3 items in a dictionary)

        [dictionary setObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange

        ] forKey:(NSString*)kCVPixelBufferPixelFormatTypeKey];

        dictionary[AVVideoColorPropertiesKey] =

            @{AVVideoColorPrimariesKey:
                AVVideoColorPrimaries_ITU_R_709_2,
            AVVideoTransferFunctionKey:
                AVVideoTransferFunction_ITU_R_709_2,
            AVVideoYCbCrMatrixKey:
                AVVideoYCbCrMatrix_ITU_R_709_2};

        AVAssetReaderTrackOutput* asset_reader_output = [[AVAssetReaderTrackOutput alloc] initWithTrack:video_track outputSettings:dictionary];

// then proceed to normally walk thru frames of the video