@@ -120,6 +120,16 @@ k4a_result_t UVCCameraReader::Start(const uint32_t width,
120120 m_output_image_format = imageFormat;
121121 m_input_image_format = K4A_IMAGE_FORMAT_COLOR_MJPG;
122122
123+ if (m_decoder == nullptr )
124+ {
125+ m_decoder = tjInitDecompress ();
126+ if (m_decoder == nullptr )
127+ {
128+ LOG_ERROR (" MJPEG decoder initialization failed\n " , 0 );
129+ return K4A_RESULT_FAILED;
130+ }
131+ }
132+
123133 frameFormat = UVC_COLOR_FORMAT_MJPEG;
124134 break ;
125135 default :
@@ -1121,6 +1131,7 @@ void UVCCameraReader::Callback(uvc_frame_t *frame)
11211131 uint32_t iso_speed = 0 ;
11221132 uint32_t white_balance = 0 ;
11231133 bool decodeMJPEG = false ;
1134+ bool drop_image = false ;
11241135
11251136 // Parse metadata
11261137 size_t bufferLeft = (size_t )frame->metadata_bytes ;
@@ -1204,6 +1215,10 @@ void UVCCameraReader::Callback(uvc_frame_t *frame)
12041215 {
12051216 // Decode MJPG into BRGA32
12061217 result = DecodeMJPEGtoBGRA32 ((uint8_t *)frame->data , frame->data_bytes , buffer, buffer_size);
1218+ if (K4A_FAILED (result))
1219+ {
1220+ drop_image = true ;
1221+ }
12071222 }
12081223 else
12091224 {
@@ -1253,8 +1268,11 @@ void UVCCameraReader::Callback(uvc_frame_t *frame)
12531268 capture_set_color_image (capture, image);
12541269 }
12551270
1256- // Calback to color
1257- m_pCallback (result, capture, m_pCallbackContext);
1271+ if (!drop_image)
1272+ {
1273+ // Calback to color
1274+ m_pCallback (result, capture, m_pCallbackContext);
1275+ }
12581276
12591277 if (image)
12601278 {
@@ -1275,16 +1293,6 @@ UVCCameraReader::DecodeMJPEGtoBGRA32(uint8_t *in_buf, const size_t in_size, uint
12751293{
12761294 RETURN_VALUE_IF_ARG (K4A_RESULT_FAILED, m_width_pixels * m_height_pixels * 4 > out_size);
12771295
1278- if (m_decoder == nullptr )
1279- {
1280- m_decoder = tjInitDecompress ();
1281- if (m_decoder == nullptr )
1282- {
1283- LOG_ERROR (" MJPEG decoder initialization failed\n " , 0 );
1284- return K4A_RESULT_FAILED;
1285- }
1286- }
1287-
12881296 int decompressStatus = tjDecompress2 (m_decoder,
12891297 in_buf,
12901298 (unsigned long )in_size,
@@ -1297,7 +1305,10 @@ UVCCameraReader::DecodeMJPEGtoBGRA32(uint8_t *in_buf, const size_t in_size, uint
12971305
12981306 if (decompressStatus != 0 )
12991307 {
1300- LOG_ERROR (" MJPEG decode failed: %d" , decompressStatus);
1308+ // This can happen when the host PC is not reading data off the camera fast enough. We also have the option to
1309+ // move the use of libjpeg-turbo to a more recent version and use tjGetErrorCode() to get a better understanding
1310+ // of the status returned.
1311+ LOG_WARNING (" MJPEG decode failed, dropping image: %d" , decompressStatus);
13011312 return K4A_RESULT_FAILED;
13021313 }
13031314
0 commit comments