From d825b7ee9a24cf8ff8aee76d286f060ee7582488 Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Thu, 23 Apr 2015 14:40:44 +0300 Subject: [PATCH] fixed mjpeg encoder so that libjpeg-based decoder does not complain on extra bytes in the end of stream --- modules/videoio/src/cap.cpp | 4 ++-- modules/videoio/src/cap_mjpeg_encoder.cpp | 21 ++++++++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/modules/videoio/src/cap.cpp b/modules/videoio/src/cap.cpp index 3a6d413a3a..1fedb0b66d 100644 --- a/modules/videoio/src/cap.cpp +++ b/modules/videoio/src/cap.cpp @@ -552,11 +552,11 @@ static Ptr IVideoCapture_create(int index) } -static Ptr IVideoCapture_create(const String&) +static Ptr IVideoCapture_create(const String& filename) { Ptr capture; - //capture = createMotionJpegCapture(filename); + capture = createMotionJpegCapture(filename); if (capture && capture->isOpened()) { return capture; diff --git a/modules/videoio/src/cap_mjpeg_encoder.cpp b/modules/videoio/src/cap_mjpeg_encoder.cpp index 7856fd416f..2a8e0b9645 100644 --- a/modules/videoio/src/cap_mjpeg_encoder.cpp +++ b/modules/videoio/src/cap_mjpeg_encoder.cpp @@ -248,6 +248,25 @@ public: writeBlock(); } + void jflush(unsigned currval, int bitIdx) + { + uchar v; + uchar* ptr = m_current; + currval |= (1 << bitIdx)-1; + while( bitIdx < 32 ) + { + v = (uchar)(currval >> 24); + *ptr++ = v; + if( v == 255 ) + *ptr++ = 0; + currval <<= 8; + bitIdx += 8; + } + m_current = ptr; + if( m_current >= m_end ) + writeBlock(); + } + static bool createEncodeHuffmanTable( const int* src, unsigned* table, int max_size ) { int i, k; @@ -1440,7 +1459,7 @@ void MotionJpegWriter::writeFrameData( const uchar* data, int step, int colorspa } // Flush - JPUT_BITS((unsigned)-1, bit_idx & 31); + strm.jflush(currval, bit_idx); strm.jputShort( 0xFFD9 ); // EOI marker /*printf("total dct = %.1fms, total cvt = %.1fms\n", total_dct*1000./cv::getTickFrequency(),