| Updated: October 28, 2024 |
The Camera library provides the necessary codecs to encode and decode image buffers to and from H.264 format and uncompressed formats.
...
...
bool maxMin;
int numSupported;
int *supportValues = NULL;
camera_handle_t cameraHandle;
camera_videocodec_t videoCodec;
int x=0;
// Connect to the camera, set the viewfinder mode for video encoding, 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 the 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");
return -1;
}
// Retrieve configured parameters on the camera
camera_get_videoencoder_parameter(cameraHandle,
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 the number of supported bitrates
camera_get_supported_videoencoder_parameter_values(cameraHandle,
CAMERA_H264AVC_LEVEL, 0,
&numSupported, NULL,
&maxMin);
// Allocate memory to hold all supported bitrate values
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 set it. For example, we'll pick the first one.
camera_set_videoencoder_parameter(cameraHandle, CAMERA_H264AVC_LEVEL, supportedValues[0]);
// Start encoding and use a callback function that keeps track 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 the viewfinder and 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++;
}
}
}
...
...
By default, the Camera library uses an encoding bitrate that lets you record video at 720p at 30 fps at 8 Mbps. You can reduce the 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, call camera_get_supported_videoencoder_parameter_values(). To determine the current bitrate set for the camera, call camera_get_videoencoder_parameter().
...
...
bool maxMin;
int numSupported;
int *supportValues = NULL;
camera_handle_t cameraHandle;
int x=0;
// Connect to the camera, set the viewfinder mode for video encoding, 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
camera_get_videoencoder_parameter(cameraHandle, CAMERA_H264AVC_BITRATE, &bitrate);
// Call it the first time to determine the number of supported bitrates
camera_get_supported_videoencoder_parameter_values(cameraHandle,
CAMERA_H264AVC_BITRATE,
0,
&numSupported,
NULL,
&maxMin);
// Allocate memory to hold all supported bitrate values
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 set it. For example, we'll pick the first one.
camera_set_videoencoder_parameter(cameraHandle, CAMERA_H264AVC_BITRATE, supportedValues[0]);
// Go encode your video
...
...