cancel
Showing results for 
Search instead for 
Did you mean: 

Issue Recording Grayscale Video in VENC_SDCard Example

athern27
Senior

 

Hi everyone,

I was exploring the VENC_SDCard example from STM32CubeN6 v1.2.0, and I was successfully able to record and store video to the SD card after making a few necessary changes mentioned here.

Now, I want to record and store the video in grayscale format. After modifying the MX_DCMIPP_Init() function in main.c and the BSP_LCD_InitEx() function in stm32n6570_discovery_led.c, I can correctly see the grayscale video output on the display.

Image.jpeg

However, when I try to record this grayscale stream, the resulting video is distorted.

(Attached it)

Upon debugging, I suspect the issue originates from the following line in encoder_prepare():

preproc_cfg.inputType = H264ENC_RGB565;

The inputType is still set to RGB, and after checking the h264encapi.h header file, I found the following enum for H264EncPictureType on line 159:

    typedef enum
    {
        H264ENC_YUV420_PLANAR = 0,              /**< YYYY... UUUU... VVVV...  */
        H264ENC_YUV420_SEMIPLANAR,              /**< YYYY... UVUVUV...        */
        H264ENC_YUV420_SEMIPLANAR_VU,           /**< YYYY... VUVUVU...        */
        H264ENC_YUV422_INTERLEAVED_YUYV,        /**< YUYVYUYV...              */
        H264ENC_YUV422_INTERLEAVED_UYVY,        /**< UYVYUYVY...              */
        H264ENC_RGB565,                         /**< 16-bit RGB 16bpp         */
        H264ENC_BGR565,                         /**< 16-bit RGB 16bpp         */
        H264ENC_RGB555,                         /**< 15-bit RGB 16bpp         */
        H264ENC_BGR555,                         /**< 15-bit RGB 16bpp         */
        H264ENC_RGB444,                         /**< 12-bit RGB 16bpp         */
        H264ENC_BGR444,                         /**< 12-bit RGB 16bpp         */
        H264ENC_RGB888,                         /**< 24-bit RGB 32bpp         */
        H264ENC_BGR888,                         /**< 24-bit RGB 32bpp         */
        H264ENC_RGB101010,                      /**< 30-bit RGB 32bpp         */
        H264ENC_BGR101010,                      /**< 30-bit RGB 32bpp         */
        H264ENC_SP_101010,                      /**< yuv420 10bit, semi-planar */
        H264ENC_P010                            /**< yuv420 10bit, planar */
    } H264EncPictureType;

Unfortunately, there's no entry for an L8 format.

How should I proceed to enable grayscale video recording and save it properly to the SD card? Any suggestions would be greatly appreciated.

Thank you for your support!

 

1 ACCEPTED SOLUTION

Accepted Solutions

Solved:

DCMIPP_ColorConversionConfTypeDef color_conf = {
      .ClampOutputSamples = ENABLE,
      .OutputSamplesType = DCMIPP_CLAMP_RGB,  // <--- important

      .RR = 77,  .RG = 150, .RB = 29,  .RA = 0,   // R = grayscale
      .GR = 77,  .GG = 150, .GB = 29,  .GA = 0,   // G = grayscale
      .BR = 77,  .BG = 150, .BB = 29,  .BA = 0    // B = grayscale
  };

 

and this is my full MX_DCMIPP_Init function for 2592x1944 resolution

static void MX_DCMIPP_Init(void)
{
  /* USER CODE BEGIN DCMIPP_Init 0 */
  /* USER CODE END DCMIPP_Init 0 */
  DCMIPP_PipeConfTypeDef pPipeConf = {0};
  DCMIPP_CSI_PIPE_ConfTypeDef pCSIPipeConf = {0};
  DCMIPP_CSI_ConfTypeDef csiconf = {0};
  DCMIPP_DownsizeTypeDef DonwsizeConf ={0};

  /* Set DCMIPP instance */
  hdcmipp.Instance = DCMIPP;
  if (HAL_DCMIPP_Init(&hdcmipp) != HAL_OK)
  {
    Error_Handler();
  }

  /* Configure the CSI */
  csiconf.DataLaneMapping = DCMIPP_CSI_PHYSICAL_DATA_LANES;
  csiconf.NumberOfLanes   = DCMIPP_CSI_TWO_DATA_LANES;
  csiconf.PHYBitrate      = DCMIPP_CSI_PHY_BT_1600;
  if(HAL_DCMIPP_CSI_SetConfig(&hdcmipp, &csiconf) != HAL_OK)
  {
    Error_Handler();
  }
  /* Configure the Virtual Channel 0 */
  /* Set Virtual Channel config */
  if(HAL_DCMIPP_CSI_SetVCConfig(&hdcmipp, DCMIPP_VIRTUAL_CHANNEL0, DCMIPP_CSI_DT_BPP10) != HAL_OK)
  {
    Error_Handler();
  }

  /* Configure the serial Pipe */
  pCSIPipeConf.DataTypeMode = DCMIPP_DTMODE_DTIDA;
  pCSIPipeConf.DataTypeIDA  = DCMIPP_DT_RAW10;
  pCSIPipeConf.DataTypeIDB  = DCMIPP_DT_RAW10; /* Don't Care */

  if (HAL_DCMIPP_CSI_PIPE_SetConfig(&hdcmipp, DCMIPP_PIPE1, &pCSIPipeConf) != HAL_OK)
  {
    Error_Handler();
  }

  pPipeConf.FrameRate  = DCMIPP_FRAME_RATE_ALL;
  pPipeConf.PixelPackerFormat = DCMIPP_PIXEL_PACKER_FORMAT_RGB565_1;

  /* Set Pitch for Main and Ancillary Pipes */
  pPipeConf.PixelPipePitch  = 1600 ; /* Number of bytes */

  /* Configure Pipe */
  if (HAL_DCMIPP_PIPE_SetConfig(&hdcmipp, DCMIPP_PIPE1, &pPipeConf) != HAL_OK)
  {
    Error_Handler();
  }

  DCMIPP_ColorConversionConfTypeDef color_conf = {
      .ClampOutputSamples = ENABLE,
      .OutputSamplesType = DCMIPP_CLAMP_RGB,  // <--- important

      .RR = 77,  .RG = 150, .RB = 29,  .RA = 0,   // R = grayscale
      .GR = 77,  .GG = 150, .GB = 29,  .GA = 0,   // G = grayscale
      .BR = 77,  .BG = 150, .BB = 29,  .BA = 0    // B = grayscale
  };

  if (HAL_DCMIPP_PIPE_SetYUVConversionConfig(&hdcmipp, DCMIPP_PIPE1, &color_conf) != HAL_OK)
    {
	  Error_Handler();
    }
    if (HAL_DCMIPP_PIPE_EnableYUVConversion(&hdcmipp, DCMIPP_PIPE1) != HAL_OK)
    {
    	Error_Handler();
    }
 
    DonwsizeConf.HRatio      = 25656;
	DonwsizeConf.VRatio      = 33161;
	DonwsizeConf.HSize       = 800;
	DonwsizeConf.VSize       = 480;
	DonwsizeConf.HDivFactor  = 316;
	DonwsizeConf.VDivFactor  = 253;

  if(HAL_DCMIPP_PIPE_SetDownsizeConfig(&hdcmipp, DCMIPP_PIPE1, &DonwsizeConf) != HAL_OK)
  {
    Error_Handler();
  }
  if(HAL_DCMIPP_PIPE_EnableDownsize(&hdcmipp, DCMIPP_PIPE1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN DCMIPP_Init 2 */
  /* USER CODE END DCMIPP_Init 2 */
}

 

Edit:
The matrix that @DanielS provided works correctly for the YUV output format. However, I'm currently working with RGB output, so the color conversion matrix needs to be different in my case.

View solution in original post

3 REPLIES 3
athern27
Senior

Can someone move this topic to STM32 MCUs Products forum
I think it's more appropriate there.

DanielS
ST Employee

Hello,

 

The easiest way to achieve this is to use DCMIPP to convert RGB to monochrome while keeping the YUV format as the encoder input format.
To accomplish this, you can adjust the color conversion matrix as follows:

 

DCMIPP_ColorConversionConfTypeDef color_conf = { .

ClampOutputSamples = ENABLE, .OutputSamplesType = DCMIPP_CLAMP_YUV, .

RR = 0, .RG = 0, .RB = 0, .RA = 128, .

GR = 47, .GG = 157, .GB = 16, .GA = 16, .BR = 0, .

BG = 0, .BB = 0, .BA = 128, };

 

HAL_DCMIPP_PIPE_SetYUVConversionConfig(..., &color_conf, ...);

 

Hope this helps,

Daniel

Solved:

DCMIPP_ColorConversionConfTypeDef color_conf = {
      .ClampOutputSamples = ENABLE,
      .OutputSamplesType = DCMIPP_CLAMP_RGB,  // <--- important

      .RR = 77,  .RG = 150, .RB = 29,  .RA = 0,   // R = grayscale
      .GR = 77,  .GG = 150, .GB = 29,  .GA = 0,   // G = grayscale
      .BR = 77,  .BG = 150, .BB = 29,  .BA = 0    // B = grayscale
  };

 

and this is my full MX_DCMIPP_Init function for 2592x1944 resolution

static void MX_DCMIPP_Init(void)
{
  /* USER CODE BEGIN DCMIPP_Init 0 */
  /* USER CODE END DCMIPP_Init 0 */
  DCMIPP_PipeConfTypeDef pPipeConf = {0};
  DCMIPP_CSI_PIPE_ConfTypeDef pCSIPipeConf = {0};
  DCMIPP_CSI_ConfTypeDef csiconf = {0};
  DCMIPP_DownsizeTypeDef DonwsizeConf ={0};

  /* Set DCMIPP instance */
  hdcmipp.Instance = DCMIPP;
  if (HAL_DCMIPP_Init(&hdcmipp) != HAL_OK)
  {
    Error_Handler();
  }

  /* Configure the CSI */
  csiconf.DataLaneMapping = DCMIPP_CSI_PHYSICAL_DATA_LANES;
  csiconf.NumberOfLanes   = DCMIPP_CSI_TWO_DATA_LANES;
  csiconf.PHYBitrate      = DCMIPP_CSI_PHY_BT_1600;
  if(HAL_DCMIPP_CSI_SetConfig(&hdcmipp, &csiconf) != HAL_OK)
  {
    Error_Handler();
  }
  /* Configure the Virtual Channel 0 */
  /* Set Virtual Channel config */
  if(HAL_DCMIPP_CSI_SetVCConfig(&hdcmipp, DCMIPP_VIRTUAL_CHANNEL0, DCMIPP_CSI_DT_BPP10) != HAL_OK)
  {
    Error_Handler();
  }

  /* Configure the serial Pipe */
  pCSIPipeConf.DataTypeMode = DCMIPP_DTMODE_DTIDA;
  pCSIPipeConf.DataTypeIDA  = DCMIPP_DT_RAW10;
  pCSIPipeConf.DataTypeIDB  = DCMIPP_DT_RAW10; /* Don't Care */

  if (HAL_DCMIPP_CSI_PIPE_SetConfig(&hdcmipp, DCMIPP_PIPE1, &pCSIPipeConf) != HAL_OK)
  {
    Error_Handler();
  }

  pPipeConf.FrameRate  = DCMIPP_FRAME_RATE_ALL;
  pPipeConf.PixelPackerFormat = DCMIPP_PIXEL_PACKER_FORMAT_RGB565_1;

  /* Set Pitch for Main and Ancillary Pipes */
  pPipeConf.PixelPipePitch  = 1600 ; /* Number of bytes */

  /* Configure Pipe */
  if (HAL_DCMIPP_PIPE_SetConfig(&hdcmipp, DCMIPP_PIPE1, &pPipeConf) != HAL_OK)
  {
    Error_Handler();
  }

  DCMIPP_ColorConversionConfTypeDef color_conf = {
      .ClampOutputSamples = ENABLE,
      .OutputSamplesType = DCMIPP_CLAMP_RGB,  // <--- important

      .RR = 77,  .RG = 150, .RB = 29,  .RA = 0,   // R = grayscale
      .GR = 77,  .GG = 150, .GB = 29,  .GA = 0,   // G = grayscale
      .BR = 77,  .BG = 150, .BB = 29,  .BA = 0    // B = grayscale
  };

  if (HAL_DCMIPP_PIPE_SetYUVConversionConfig(&hdcmipp, DCMIPP_PIPE1, &color_conf) != HAL_OK)
    {
	  Error_Handler();
    }
    if (HAL_DCMIPP_PIPE_EnableYUVConversion(&hdcmipp, DCMIPP_PIPE1) != HAL_OK)
    {
    	Error_Handler();
    }
 
    DonwsizeConf.HRatio      = 25656;
	DonwsizeConf.VRatio      = 33161;
	DonwsizeConf.HSize       = 800;
	DonwsizeConf.VSize       = 480;
	DonwsizeConf.HDivFactor  = 316;
	DonwsizeConf.VDivFactor  = 253;

  if(HAL_DCMIPP_PIPE_SetDownsizeConfig(&hdcmipp, DCMIPP_PIPE1, &DonwsizeConf) != HAL_OK)
  {
    Error_Handler();
  }
  if(HAL_DCMIPP_PIPE_EnableDownsize(&hdcmipp, DCMIPP_PIPE1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN DCMIPP_Init 2 */
  /* USER CODE END DCMIPP_Init 2 */
}

 

Edit:
The matrix that @DanielS provided works correctly for the YUV output format. However, I'm currently working with RGB output, so the color conversion matrix needs to be different in my case.