Configure the video encoding

Updated: April 19, 2023

If you're recording AVC1 or H.264 video, you can configure the video encoding.

Before you set any video encoder parameters, you can select the type of encoder to use. This can be either a local encoder, meaning a hardware video encoder on the local system, or a remote encoder, meaning a connected camera that supports video encoding natively. For both settings, the configuration is done the same way, using the camera_encoder.h and camera_h264avc.h functions.

Note: The code below contains excerpts from the Camera example application, specifically from the setVideoEncoderConfiguration() function in video.cpp. The full source code of this application is available in the QNX SDP 7.1 Sensor Framework Base package.
...
...
camera_handle_t cameraHandle;
int err;

bool propertyChange = false;
camera_videocodec_t videoCodec;
int bitrate;

char userString[32];
uint32_t encoderSelection = CAMERA_ENCODER_LOCAL;
int userValue;
bool maxMin;

if (camera_has_feature(handle, CAMERA_FEATURE_CONFIGURE_REMOTE_ENCODER)) {
    // Get which encoder we are currently configuring
    err = camera_get_video_property(handle, CAMERA_IMGPROP_ENCODERSELECT, &encoderSelection);

    printf("The %s encoder is currently selected, do you want to select another one to configure (y/n)?\n",
            encoderSelection == CAMERA_ENCODER_LOCAL ? "local":"remote");
    fgets(userString, sizeof(userString), stdin);
    if ( (userString[0] == 'y') || (userString[0] == 'Y') ) {
        // Ask user which video encoder they want to configure
        printf("Enter the name of the encoder you want to configure:\n");
        printf("\t%d) %s\n", CAMERA_ENCODER_LOCAL, "local");
        printf("\t%d) %s\n", CAMERA_ENCODER_REMOTE, "remote");

        fgets(userString, sizeof(userString), stdin);
        userValue = atoi(userString);

        if (userValue != CAMERA_ENCODER_LOCAL && userValue != CAMERA_ENCODER_REMOTE) {
            printf("Invalid option selected, leaving it unchanged\n");
        } else {
            // Set the video to use the encoder requested by the user
            encoderSelection = userValue;
            err = camera_set_video_property(handle, CAMERA_IMGPROP_ENCODERSELECT, encoderSelection);
            if (err != EOK) {
                printf("Failed to select video encoder to configure: err = %d\n", err);
                return err;
            }
        }
    }
}

// Verify that the user selected the H.264 or AVC1 codec
err = camera_get_video_property(handle, CAMERA_IMGPROP_VIDEOCODEC, &videoCodec);
if (err != EOK) {
    printf("Failed to read the configured video codec: err = %d\n", err);
    return err;
}
if ( (videoCodec != CAMERA_VIDEOCODEC_H264) && (videoCodec != CAMERA_VIDEOCODEC_AVC1) ) {
    printf("No video encoder configuration supported for selected codec\n");
    return EOK;
}

// Retrieve one of the currently configured parameter values
err = camera_get_videoencoder_parameter(handle, CAMERA_H264AVC_BITRATE, &bitrate);
if (err != EOK) {
    printf("Failed to retrieve video encoder parameters: err = %d\n", err);
    return err;
}

if (isParameterSupported(handle, CAMERA_H264AVC_BITRATE)) {
    // Permit the user to select the encoding bitrate
    printf("The bitrate is currently %d bps (0 is default), do you want to modify (y/n)?\n", bitrate);
    fgets(userString, sizeof(userString), stdin);
    if ( (userString[0] == 'y') || (userString[0] == 'Y') ) {
        err = modifyVideoEncoderParameter(handle, CAMERA_H264AVC_BITRATE, &bitrate, false);
        if (err != EOK) {
            return err;
        }
        propertyChange = true;
    }
}

// There are other video encoder parameters whose settings you can query and then update based on
// user input; you would put the code for doing so here. See camera_h264avc_parameters_t for a list of
// the supported parameters. See the actual setVideoEncoderConfiguration() function in video.cpp for 
// the code that updates them.

// Apply any new values that have been modified
if (propertyChange == true) {
    err = camera_set_videoencoder_parameter(handle, CAMERA_H264AVC_BITRATE, bitrate);
    if (err != EOK) {
        printf("Failed to set the new video encoder parameters: err = %d\n", err);
        return err;
    }
}
...
...