@@ -211,11 +211,7 @@ void MFWorker::runMe()
211211 if (_isActive && _width > 0 && _height > 0 )
212212 {
213213 if (_pixelFormat == PixelFormat::MJPEG)
214- {
215- if (_qframe)
216- {
217- _cropLeft = _cropRight = _cropTop = _cropBottom = 0 ;
218- }
214+ {
219215 process_image_jpg_mt ();
220216 }
221217 else
@@ -265,75 +261,78 @@ void MFWorker::noBusy()
265261 _isBusy = false ;
266262}
267263
268-
269264void MFWorker::process_image_jpg_mt ()
270265{
271266 if (_decompress == nullptr )
272267 _decompress = tjInitDecompress ();
273268
274- if (tjDecompressHeader2 (_decompress, const_cast <uint8_t *>(_localData), _size, &_width, &_height, &_subsamp) != 0 )
269+ if (tjDecompressHeader2 (_decompress, const_cast <uint8_t *>(_localData), _size, &_width, &_height, &_subsamp) != 0 &&
270+ tjGetErrorCode (_decompress) == TJERR_FATAL)
275271 {
276- if (tjGetErrorCode (_decompress) == TJERR_FATAL)
277- {
278- QString info = QString (tjGetErrorStr ());
279- emit newFrameError (_workerIndex, info, _currentFrame);
280- return ;
281- }
272+ emit newFrameError (_workerIndex, QString (tjGetErrorStr ()), _currentFrame);
273+ return ;
274+ }
275+
276+ if (_subsamp != TJSAMP_422 && _hdrToneMappingEnabled > 0 )
277+ {
278+ emit newFrameError (_workerIndex, QString (" %1: %2" ).arg (UNSUPPORTED_DECODER).arg (_subsamp), _currentFrame);
279+ return ;
282280 }
283281
284282 tjscalingfactor sca{ 1 , 2 };
285- Image<ColorRgb> srcImage ((_qframe) ? TJSCALED (_width, sca) : _width,
286- (_qframe) ? TJSCALED (_height, sca) : _height);
287- _width = srcImage.width ();
288- _height = srcImage.height ();
289283
290- if (tjDecompress2 (_decompress, const_cast <uint8_t *>(_localData), _size, (uint8_t *)srcImage.memptr (), _width, 0 , _height, TJPF_RGB, TJFLAG_FASTDCT | TJFLAG_FASTUPSAMPLE) != 0 )
291- {
292- if (tjGetErrorCode (_decompress) == TJERR_FATAL)
293- {
294- QString info = QString (tjGetErrorStr ());
295- emit newFrameError (_workerIndex, info, _currentFrame);
296- return ;
297- }
298- }
284+ _width = (_qframe) ? TJSCALED (_width, sca) : _width;
285+ _height = (_qframe) ? TJSCALED (_height, sca) : _height;
299286
300- // got image, process it
301- if (!(_cropLeft > 0 || _cropTop > 0 || _cropBottom > 0 || _cropRight > 0 ))
302- {
303- // apply LUT
304- FrameDecoder::applyLUT ((unsigned char *)srcImage.memptr (), srcImage.width (), srcImage.height (), _lutBuffer, _hdrToneMappingEnabled);;
287+ Image<ColorRgb> image (_width - _cropLeft - _cropRight, _height - _cropTop - _cropBottom);
305288
306- // exit
307- emit newFrame (_workerIndex, srcImage, _currentFrame, _frameBegin);
308- }
309- else
289+ if (_hdrToneMappingEnabled > 0 )
310290 {
311- // calculate the output size
312- int outputWidth = (_width - _cropLeft - _cropRight);
313- int outputHeight = (_height - _cropTop - _cropBottom);
291+ size_t yuvSize = tjBufSizeYUV2 (_width, 2 , _height, _subsamp);
292+ uint8_t * jpegBuffer = (uint8_t *)malloc (yuvSize);
314293
315- if (outputWidth <= 0 || outputHeight <= 0 )
316- {
317- QString info = QString (" Invalid cropping" );
318- emit newFrameError (_workerIndex, info, _currentFrame);
319- return ;
320- }
321-
322- Image<ColorRgb> destImage (outputWidth, outputHeight);
294+ if (tjDecompressToYUV2 (_decompress, const_cast <uint8_t *>(_localData), _size, jpegBuffer, _width, 2 , _height, TJFLAG_FASTDCT | TJFLAG_FASTUPSAMPLE) != 0 &&
295+ tjGetErrorCode (_decompress) == TJERR_FATAL)
296+ {
297+ free (jpegBuffer);
323298
324- for (unsigned int y = 0 ; y < destImage.height (); y++)
325- {
326- unsigned char * source = (unsigned char *)srcImage.memptr () + (static_cast <size_t >(y) + _cropTop) * srcImage.width () * 3 + static_cast <size_t >(_cropLeft) * 3 ;
327- unsigned char * dest = (unsigned char *)destImage.memptr () + static_cast <size_t >(y) * destImage.width () * 3 ;
328- memcpy (dest, source, static_cast <size_t >(destImage.width ()) * 3 );
329- }
299+ emit newFrameError (_workerIndex, QString (tjGetErrorStr ()), _currentFrame);
300+ return ;
301+ }
330302
331- // apply LUT
332- FrameDecoder::applyLUT (( unsigned char *)destImage. memptr (), destImage. width (), destImage. height (), _lutBuffer, _hdrToneMappingEnabled );
303+ FrameDecoder::processImage (_cropLeft, _cropRight, _cropTop, _cropBottom,
304+ jpegBuffer, _width, _height, _width, PixelFormat::MJPEG, _lutBuffer, image );
333305
334- // exit
335- emit newFrame (_workerIndex, destImage, _currentFrame, _frameBegin);
306+ free (jpegBuffer);
336307 }
337- }
308+ else if (image.width () != (uint)_width || image.height () != (uint)_height)
309+ {
310+ uint8_t * jpegBuffer = (uint8_t *)malloc (_width * _height * 3 );
311+
312+ if (tjDecompress2 (_decompress, const_cast <uint8_t *>(_localData), _size, (uint8_t *)jpegBuffer, _width, 0 , _height, TJPF_BGR, TJFLAG_BOTTOMUP | TJFLAG_FASTDCT | TJFLAG_FASTUPSAMPLE) != 0 &&
313+ tjGetErrorCode (_decompress) == TJERR_FATAL)
314+ {
315+ free (jpegBuffer);
316+
317+ emit newFrameError (_workerIndex, QString (tjGetErrorStr ()), _currentFrame);
318+ return ;
319+ }
320+
321+ FrameDecoder::processImage (_cropLeft, _cropRight, _cropTop, _cropBottom,
322+ jpegBuffer, _width, _height, _width * 3 , PixelFormat::RGB24, nullptr , image);
338323
324+ free (jpegBuffer);
325+ }
326+ else
327+ {
328+ if (tjDecompress2 (_decompress, const_cast <uint8_t *>(_localData), _size, (uint8_t *)image.memptr (), _width, 0 , _height, TJPF_RGB, TJFLAG_FASTDCT | TJFLAG_FASTUPSAMPLE) != 0 &&
329+ tjGetErrorCode (_decompress) == TJERR_FATAL)
330+ {
331+ emit newFrameError (_workerIndex, QString (tjGetErrorStr ()), _currentFrame);
332+ return ;
333+ }
334+ }
335+
339336
337+ emit newFrame (_workerIndex, image, _currentFrame, _frameBegin);
338+ }
0 commit comments