Skip to content

Commit

Permalink
Add ISP Control API and Examples (#174)
Browse files Browse the repository at this point in the history
- Update CameraSetting in VideoStream.cpp,.h
- Add Examples for Image Tuning, Mode, AWB. WDR and AE
  • Loading branch information
pammyleong committed Dec 15, 2023
1 parent 10ce50f commit 7ca4ffe
Show file tree
Hide file tree
Showing 9 changed files with 997 additions and 1 deletion.
29 changes: 29 additions & 0 deletions Arduino_package/hardware/cores/ambpro2/video_drv.c
Expand Up @@ -2,6 +2,7 @@
#include "hal_video.h"
#include "video_api.h"
#include "module_video.h"
#include "isp_ctrl_api.h"

extern int incb[5];
extern int enc_queue_cnt[5];
Expand All @@ -10,6 +11,21 @@ uint32_t image_addr = 0;
uint32_t image_len = 0;
int voe_heap_size = 0;

static isp_control ISPCtrl = {
.Brightness = 0,
.Contrast = 50,
.Saturation = 50,
.Sharpness = 50,
.LDC = 0,
.WDRMode = 0,
.WDRLevel = 50,
.ExposureMode = 1,
.AWB = 1,
.GrayMode = 0,
.PowerLineFreq = 3,
.DayNightMode = 0
};

static video_params_t video_params = {
.stream_id = 0,
.type = 0,
Expand Down Expand Up @@ -45,6 +61,19 @@ static video_params_t video_v4_params = {
}
};

void ISPControlReset(void) {
isp_set_brightness(ISPCtrl.Brightness);
isp_set_contrast(ISPCtrl.Contrast);
isp_set_saturation(ISPCtrl.Saturation);
isp_set_sharpness(ISPCtrl.Sharpness);
isp_set_ldc(ISPCtrl.LDC);
isp_set_wdr_mode(ISPCtrl.WDRMode);
isp_set_exposure_mode(ISPCtrl.ExposureMode);
isp_set_awb_ctrl(ISPCtrl.AWB);
isp_set_gray_mode(ISPCtrl.GrayMode);
isp_set_power_line_freq(ISPCtrl.PowerLineFreq);
isp_set_day_night(ISPCtrl.DayNightMode);
}

int cameraConfig(int v1_enable, int v1_w, int v1_h, int v1_bps, int v1_snapshot,
int v2_enable, int v2_w, int v2_h, int v2_bps, int v2_snapshot,
Expand Down
21 changes: 21 additions & 0 deletions Arduino_package/hardware/cores/ambpro2/video_drv.h
Expand Up @@ -3,6 +3,27 @@

#include "mmf2_module.h"

typedef struct {
int Brightness;
int Contrast;
int Saturation;
int Sharpness;
int LDC;
int WDRMode;
int WDRLevel;
int ExposureMode;
int ExposureTime;
int AWB;
int RedBalance;
int BlueBalance;
int AEGain;
int GrayMode;
int PowerLineFreq;
int DayNightMode;
} isp_control;

void ISPControlReset(void);

int cameraConfig(int v1_enable, int v1_w, int v1_h, int v1_bps, int v1_snapshot,
int v2_enable, int v2_w, int v2_h, int v2_bps, int v2_snapshot,
int v3_enable, int v3_w, int v3_h, int v3_bps, int v3_snapshot,
Expand Down
@@ -0,0 +1,121 @@
/*
Example guide:
// "AE=" Set Auto Exposure, 0: Manual, 1: Auto
// "AE" Get value of Auto Exposure
// "EXPTIME=" Set Exposure Time: Max: 33333us
// "EXPTIME" Get Exposure Time
// "GAIN=" Set AE Gain: Range from 256 to 32768
// "GAIN" Get AE Gain
// "PLF=" Set Power Line Frequency, 0: Disabled, 1: 50Hz, 2: 60Hz, 3: Auto
// "PLF" Get value of Power Line Frequency
// "RESET" Reset to default
*/

#include "WiFi.h"
#include "StreamIO.h"
#include "VideoStream.h"
#include "RTSP.h"

#define CHANNEL 0

// Default preset configurations for each video channel:
// Channel 0 : 1920 x 1080 30FPS H264
// Channel 1 : 1280 x 720 30FPS H264
// Channel 2 : 1280 x 720 30FPS MJPEG
CameraSetting configCam;
VideoSetting config(CHANNEL);
RTSP rtsp;
StreamIO videoStreamer(1, 1); // 1 Input Video -> 1 Output RTSP

char ssid[] = "Network_SSID"; // your network SSID (name)
char pass[] = "Password"; // your network password
int status = WL_IDLE_STATUS;

void setup() {
Serial.begin(115200);

// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);

// wait 2 seconds for connection:
delay(2000);
}

// Configure camera video channel with video format information
// Adjust the bitrate based on your WiFi network quality
//config.setBitrate(2 * 1024 * 1024); // Recommend to use 2Mbps for RTSP streaming to prevent network congestion
Camera.configVideoChannel(CHANNEL, config);
Camera.videoInit();

// Configure RTSP with identical video format information
rtsp.configVideo(config);
rtsp.begin();

// Configure StreamIO object to stream data from video channel to RTSP
videoStreamer.registerInput(Camera.getStream(CHANNEL));
videoStreamer.registerOutput(rtsp);
if (videoStreamer.begin() != 0) {
Serial.println("StreamIO link start failed");
}

// Start data stream from video channel
Camera.channelBegin(CHANNEL);

delay(1000);
printInfo();
}

void loop() {
if (Serial.available() > 0) {
String input = Serial.readString();
input.trim();

if (input.startsWith(String("AE="))) {
String value = input.substring(3);
int val = value.toInt();
configCam.setExposureMode(val);
} else if (input.startsWith(String("AE"))) {
configCam.getExposureMode();
} else if (input.startsWith(String("EXPTIME="))) {
String value = input.substring(8);
int val = value.toInt();
configCam.setExposureTime(val);
} else if (input.startsWith(String("EXPTIME"))) {
configCam.getExposureTime();
} else if (input.startsWith(String("GAIN="))) {
String value = input.substring(5);
int val = value.toInt();
configCam.setAEGain(val);
} else if (input.startsWith(String("GAIN"))) {
configCam.getAEGain();
} else if (input.startsWith(String("PLF="))) {
String value = input.substring(4);
int val = value.toInt();
configCam.setPowerLineFreq(val);
} else if (input.startsWith(String("PLF"))) {
configCam.getPowerLineFreq();
} else if (input.startsWith(String("RESET"))) {
configCam.reset();
}
}
}

void printInfo(void) {
Serial.println("------------------------------");
Serial.println("- Summary of Streaming -");
Serial.println("------------------------------");
Camera.printInfo();

IPAddress ip = WiFi.localIP();

Serial.println("- RTSP -");
Serial.print("rtsp://");
Serial.print(ip);
Serial.print(":");
rtsp.printInfo();
}

@@ -0,0 +1,128 @@
/*
Example guide:
// "BRIGHTNESS=" Set Brightness: Range from -64 to 64
// "BRIGHTNESS" Get Brightness level
// "CONTRAST=" Set Contrast: Range from 0 to 100
// "CONTRAST" Get Contrast level
// "SATURATION=" Set Contrast: Range from 0 to 100
// "SATURATION" Get Contrast level
// "SHARPNESS=" Set Sharpness: Range from 0 to 100
// "SHARPNESS" Get Sharpness level
// "LDC=" Len Distortion Correction: 0: Disabled, 1: Enabled
// "LDC" Get value of Len Distortion Correction
// "RESET" Reset to default
*/

#include "WiFi.h"
#include "StreamIO.h"
#include "VideoStream.h"
#include "RTSP.h"

#define CHANNEL 0

// Default preset configurations for each video channel:
// Channel 0 : 1920 x 1080 30FPS H264
// Channel 1 : 1280 x 720 30FPS H264
// Channel 2 : 1280 x 720 30FPS MJPEG
CameraSetting configCam;
VideoSetting config(CHANNEL);
RTSP rtsp;
StreamIO videoStreamer(1, 1); // 1 Input Video -> 1 Output RTSP

char ssid[] = "Network_SSID"; // your network SSID (name)
char pass[] = "Password"; // your network password
int status = WL_IDLE_STATUS;

void setup() {
Serial.begin(115200);

// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);

// wait 2 seconds for connection:
delay(2000);
}

// Configure camera video channel with video format information
// Adjust the bitrate based on your WiFi network quality
//config.setBitrate(2 * 1024 * 1024); // Recommend to use 2Mbps for RTSP streaming to prevent network congestion
Camera.configVideoChannel(CHANNEL, config);
Camera.videoInit();

// Configure RTSP with identical video format information
rtsp.configVideo(config);
rtsp.begin();

// Configure StreamIO object to stream data from video channel to RTSP
videoStreamer.registerInput(Camera.getStream(CHANNEL));
videoStreamer.registerOutput(rtsp);
if (videoStreamer.begin() != 0) {
Serial.println("StreamIO link start failed");
}

// Start data stream from video channel
Camera.channelBegin(CHANNEL);

delay(1000);
printInfo();
}

void loop() {
if (Serial.available() > 0) {
String input = Serial.readString();
input.trim();

if (input.startsWith(String("BRIGHTNESS="))) {
String value = input.substring(11);
int val = value.toInt();
configCam.setBrightness(val);
} else if (input.startsWith(String("BRIGHTNESS"))) {
configCam.getBrightness();
} else if (input.startsWith(String("CONTRAST="))) {
String value = input.substring(9);
int val = value.toInt();
configCam.setContrast(val);
} else if (input.startsWith(String("CONTRAST"))) {
configCam.getContrast();
} else if (input.startsWith(String("SATURATION="))) {
String value = input.substring(11);
int val = value.toInt();
configCam.setSaturation(val);
} else if (input.startsWith(String("SATURATION"))) {
configCam.getSaturation();
} else if (input.startsWith(String("SHARPNESS="))) {
String value = input.substring(10);
int val = value.toInt();
configCam.setSharpness(val);
} else if (input.startsWith(String("SHARPNESS"))) {
configCam.getSharpness();
} else if (input.startsWith(String("LDC="))) {
String value = input.substring(4);
int val = value.toInt();
configCam.setLDC(val);
} else if (input.startsWith(String("LDC"))) {
configCam.getLDC();
} else if (input.startsWith(String("RESET"))) {
configCam.reset();
}
}
}

void printInfo(void) {
Serial.println("------------------------------");
Serial.println("- Summary of Streaming -");
Serial.println("------------------------------");
Camera.printInfo();

IPAddress ip = WiFi.localIP();

Serial.println("- RTSP -");
Serial.print("rtsp://");
Serial.print(ip);
Serial.print(":");
rtsp.printInfo();
}

0 comments on commit 7ca4ffe

Please sign in to comment.