Encode video from a camera

You can encode your image buffers to H.264 format or uncompressed video. The Camera library provides the necessary codecs to encode and decode image buffers to and from H.264 format and uncompressed formats.

To encode your video:
  1. Open the camera.
  2. Set the viewfinder mode for camera.
  3. Set up Screen. See Use Screen with the Camera API for more information.
  4. Start the viewfinder and configure the camera settings as necessary as described in Start viewfinder and configure camera settings in this guide.
  5. If you want to encode to H.264, validate it's supported. To do this, use camera_get_video_property() and CAMERA_IMGPROP_VIDEOCODEC property as an argument.
  6. Start encoding video by calling camera_start_encode() and specifying a callback to handle the frames to encode. For information about the arguments to use for the callback, see Using callback mode in the Image Buffer Access chapter of this guide.
  7. When your done encoding, call camera_stop_encode() to stop encoding.
  8. Stop the viewfinder and close the camera when you are done encoding. For more information, see Stop viewfinder and close camera.”
Here's a code snippet that demonstrates how to encode video. For more information, see the Camera example application provided with this release:
...
...
bool maxMin;
int numSupported;
int *supportValues = NULL;
camera_handle_t cameraHandle;
camera_videocodec_t videoCodec;
supportedValues
int x=0;

// Connect to the camera, set the viewfinder mode, and start the viewfinder
camera_open(CAMERA_UNIT_1, CAMERA_MODE_RW | CAMERA_MODE_ROLL, &cameraHandle);
camera_set_vf_mode(cameraHandle, CAMERA_VFMODE_VIDEO);
camera_start_viewfinder(cameraHandle, NULL, NULL, NULL);

//check that video encoder configuration supports H.264
camera_get_video_property(cameraHandle, CAMERA_IMGPROP_VIDEOCODEC, &videoCodec);
if (videoCodec != CAMERA_VIDEOCODEC_H264)
{
 printf("No video encoder configuration supported for selected codec\n");
}

// Retrieve configured parameters on the camera
camera_get_videoencoder_parameter(handle,
                                  CAMERA_H264AVC_BITRATE, &bitrate,
                                  CAMERA_H264AVC_KEYFRAMEINTERVAL, &kfi,
                                  CAMERA_H264AVC_SLICETYPE, &sliceType,
                                  CAMERA_H264AVC_SLICESIZE, &sliceSize,
                                  CAMERA_H264AVC_PROFILE, &profile,
                                  CAMERA_H264AVC_LEVEL, &level,
                                  CAMERA_H264AVC_ENTROPYCODING, &entropy,
                                  CAMERA_H264AVC_RATECONTROL,&rateControl,
                                  CAMERA_H264AVC_QPI, &qpI,
                                  CAMERA_H264AVC_QPP, &qpP);
// Call it the first time to determine number of supported bitrates
camera_get_supported_videoencoder_parameter_values(cameraHandle,
                                                   CAMERA_H264AVC_LEVEL, 0, 
                                                   &numSupported, NULL, 
                                                   &maxMin);
// Allocate memory so that the valid values can be populated
supportedValues = (int*) malloc(sizeof(int) * numSupported);

// Call it a second time to retrieve the list of values
if (camera_get_supported_videoencoder_parameter_values(cameraHandle,
                                                       CAMERA_H264AVC_LEVEL, numSupported,
                                                       &numSupported, supportedValues,
                                                       &maxMin)!= EOK) {
    for(x=0; x < numSupported; ++x) {
           printf("\t Enumerator Value: %d", supportedValues[x]);
     }
}
//Pick some value and then set it. For example, we'll choose the first one.
camera_set_videoencoder_parameter(cameraHandle, CAMERA_H264AVC_LEVEL, supportedValues[0]);

//Start encoding and use a callback to call a function that keeps tracks of stats for the camera
camera_start_encode(cameraHandle[i], NULL, encodedVideoCallback, NULL, NULL, (void*)i);
...
...
// Do something while you are waiting
wait(2000);

camera_stop_encode(cameraHandle);

...
...


//Close viewfinder and then disconnect from the camera
wait(2000);
camera_stop_viewfinder(cameraHandle);
camera_close(cameraHandle);

/*
 * Encoded video callback - called with encoded video frames
 */
void encodedVideoCallback(camera_handle_t handle, camera_buffer_t* buffer, void *arg)
{
    // Print first encoded frame received and keep stats
    int     cameraInst = (int) arg;

    if (cameraInst < MAX_SIMULTANEOUS_CAMERAS) {
        if (encodingInfo[cameraInst].numFrames == 0) {
            printf("Camera %d: first encoded frame of %lld bytes key %d rate %lld\n", cameraInst + 1,
                   buffer->framedesc.compvid.bufsize, buffer->framedesc.compvid.keyframe,
                   buffer->framedesc.compvid.bitrate);
        }
        encodingInfo[cameraInst].numFrames++;
        if (buffer->framedesc.compvid.keyframe) {
            encodingInfo[cameraInst].numKeyFrames++;
        }
    }
}
...
...

Change the bitrate speed

By default, the Camera library uses an encoding bitrate that allows you to record video at 720p at 30fps at 8 Mbps. You can reduce bitrate to help increase the frames per second (fps) that you capture on the camera. For instance, you may want to encode at 4 Mbps or 2 Mbps. To do this, use camera_set_videoencoder_parameter() and the CAMERA_H264AVC_BITRATE to modify the bitrate. To determine the valid values you can use, use the camera_get_supported_videoencoder_parameter. To determine the bitrate that the camera is set, use camera_get_videoencoder_parameter(). Here's a code snippet:
...
...

bool maxMin;
int numSupported;
int *supportValues = NULL;
camera_handle_t cameraHandle;
supportedValues
int x=0;

// Connect to the camera, set the viewfinder mode, and start the viewfinder
camera_open(CAMERA_UNIT_1, CAMERA_MODE_RW | CAMERA_MODE_ROLL, &cameraHandle);
camera_set_vf_mode(cameraHandle, CAMERA_VFMODE_VIDEO);
camera_start_viewfinder(cameraHandle, NULL, NULL, NULL);

// Retrieve the current bitrate setting on the camera. T
camera_get_videoencoding_parameter(cameraHandle,
                                   CAMERA_H264AVC_BITRATE, &bitrate);
// Call it the first time to determine number of supported bitrates
camera_get_supported_videoencoder_parameter_values(cameraHandle, CAMERA_H264AVC_BITRATE, 0, &numSupported, NULL, &maxMin);
// Allocate memory so that the valid values can be populated
supportedValues = (int*) malloc(sizeof(int) * numSupported);
// Call it a second time to retrieve the list of values
if (camera_get_supported_videoencoder_parameter_values(cameraHandle, CAMERA_H264AVC_BITRATE, numSupported,
                                                        &numSupported, supportedValues, &maxMin)!= EOK) {
    for(x=0; x < numSupported; ++x) {
           printf("\t Enumerator Value: %d", supportedValues[x]);
     }
}
//Pick some value and then set it. For example, we'll choose the first one for this example.
camera_set_videoencoder_parameter(cameraHandle, CAMERA_H264AVC_BITRATE, supportedValues[0]);
...
...
// Go and encode your video
...
...