1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // INTEL CORPORATION PROPRIETARY INFORMATION
4 // This software is supplied under the terms of a license agreement or
5 // nondisclosure agreement with Intel Corporation and may not be copied
6 // or disclosed except in accordance with the terms of that agreement.
7 // Copyright (c) 1999 Intel Corporation. All Rights Reserved.
10 // Source: rd_jpeg.cpp$
14 // Authors: Vadim Pisarevsky
23 ////////////////////// JPEG-oriented two-level bitstream ////////////////////////
25 rm_jpeg_bit_stream::rm_jpeg_bit_stream()
29 rm_jpeg_bit_stream::~rm_jpeg_bit_stream()
34 bool rm_jpeg_bit_stream::open( const char* filename )
39 m_is_opened = m_low_strm.open( filename );
40 if( m_is_opened ) set_pos(0);
45 void rm_jpeg_bit_stream::close()
52 void rm_jpeg_bit_stream::read_block()
54 byte* end = m_start + m_block_size;
55 byte* current = m_start;
59 int sz = m_unget_size;
60 memmove( current - sz, m_end - sz, sz );
61 while( current < end )
63 int val = m_low_strm.get_byte();
66 *current++ = (byte)val;
70 val = m_low_strm.get_byte();
73 else if( !(0xD0 <= val && val <= 0xD7) )
75 m_low_strm.set_pos( m_low_strm.get_pos() - 2 );
84 if( current == m_start ) throw;
87 m_end = m_start + (((current - m_start) + 3) & -4);
88 bs_bswap_block( m_start, m_end );
92 void rm_jpeg_bit_stream::flush()
94 m_end = m_start + m_block_size;
95 m_current = m_end - 4;
99 void rm_jpeg_bit_stream::align_on_byte()
104 int rm_jpeg_bit_stream::find_marker()
106 int code = m_low_strm.get_word();
107 while( (code & 0xFF00) != 0xFF00 || (code == 0xFFFF || code == 0xFF00 ))
109 code = ((code&255) << 8) | m_low_strm.get_byte();
115 /****************************** JPEG (JFIF) reader ***************************/
117 // zigzag & IDCT prescaling (AAN algorithm) tables
118 static const byte zigzag[] =
120 0, 8, 1, 2, 9, 16, 24, 17, 10, 3, 4, 11, 18, 25, 32, 40,
121 33, 26, 19, 12, 5, 6, 13, 20, 27, 34, 41, 48, 56, 49, 42, 35,
122 28, 21, 14, 7, 15, 22, 29, 36, 43, 50, 57, 58, 51, 44, 37, 30,
123 23, 31, 38, 45, 52, 59, 60, 53, 46, 39, 47, 54, 61, 62, 55, 63,
124 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63
127 const int idct_prescale[] =
129 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
130 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
131 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
132 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
133 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
134 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
135 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
136 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
140 #define fix(x, n) (int)((x)*(1 << (n)) + .5)
142 #define C1_082 fix( 1.082392200, fixb )
143 #define C1_414 fix( 1.414213562, fixb )
144 #define C1_847 fix( 1.847759065, fixb )
145 #define C2_613 fix( 2.613125930, fixb )
148 #define b_cb fix( 1.772, fixc )
149 #define g_cb -fix( 0.34414, fixc )
150 #define g_cr -fix( 0.71414, fixc )
151 #define r_cr fix( 1.402, fixc )
153 // IDCT without prescaling
154 static void aan_idct8x8( int* src, int *dst, int pitch )
156 int workspace[64], *work = workspace;
159 /* Pass 1: process rows */
160 for( i = 8; i > 0; i--, src += 8, work += 8 )
163 int x0 = src[5], x1 = src[3];
164 int x2 = src[1], x3 = src[7];
166 int x4 = x0 + x1; x0 -= x1;
168 x1 = x2 + x3; x2 -= x3;
169 x3 = x1 + x4; x1 -= x4;
171 x4 = (x0 + x2)*C1_847;
172 x0 = descale( x4 - x0*C2_613, fixb);
173 x2 = descale( x2*C1_082 - x4, fixb);
174 x1 = descale( x1*C1_414, fixb);
180 work[7] = x3; work[6] = x0;
181 work[5] = x1; work[4] = x2;
183 x2 = src[2]; x3 = src[6];
184 x0 = src[0]; x1 = src[4];
187 x2 = descale((x2-x3)*C1_414, fixb) - x4;
189 x3 = x0 + x1; x0 -= x1;
190 x1 = x3 + x4; x3 -= x4;
191 x4 = x0 + x2; x0 -= x2;
194 x1 -= x2; x2 = 2*x2 + x1;
195 work[7] = x1; work[0] = x2;
198 x1 = x4 + x2; x4 -= x2;
199 work[1] = x1; work[6] = x4;
201 x1 = work[5]; x2 = work[4];
202 x4 = x0 + x1; x0 -= x1;
203 x1 = x3 + x2; x3 -= x2;
205 work[2] = x4; work[5] = x0;
206 work[3] = x3; work[4] = x1;
209 /* Pass 2: process columns */
211 for( i = 8; i > 0; i--, dst+=pitch, work++ )
214 int x0 = work[8*5], x1 = work[8*3];
215 int x2 = work[8*1], x3 = work[8*7];
217 int x4 = x0 + x1; x0 -= x1;
218 x1 = x2 + x3; x2 -= x3;
219 x3 = x1 + x4; x1 -= x4;
221 x4 = (x0 + x2)*C1_847;
222 x0 = descale( x4 - x0*C2_613, fixb);
223 x2 = descale( x2*C1_082 - x4, fixb);
224 x1 = descale( x1*C1_414, fixb);
230 dst[7] = x3; dst[6] = x0;
231 dst[5] = x1; dst[4] = x2;
233 x2 = work[8*2]; x3 = work[8*6];
234 x0 = work[8*0]; x1 = work[8*4];
237 x2 = descale((x2-x3)*C1_414, fixb) - x4;
239 x3 = x0 + x1; x0 -= x1;
240 x1 = x3 + x4; x3 -= x4;
241 x4 = x0 + x2; x0 -= x2;
244 x1 -= x2; x2 = 2*x2 + x1;
248 dst[7] = x1; dst[0] = x2;
251 x1 = descale(x4 + x2,3);
252 x4 = descale(x4 - x2,3);
253 dst[1] = x1; dst[6] = x4;
255 x1 = dst[5]; x2 = dst[4];
257 x4 = descale(x0 + x1,3);
258 x0 = descale(x0 - x1,3);
259 x1 = descale(x3 + x2,3);
260 x3 = descale(x3 - x2,3);
262 dst[2] = x4; dst[5] = x0;
263 dst[3] = x3; dst[4] = x1;
268 static const int max_dec_htable_size = 1 << 12;
269 static const int first_table_bits = 9;
271 grfmt_jpeg_reader::grfmt_jpeg_reader()
274 m_signature ="\xFF\xD8\xFF";
275 m_description = "JFIF files (*.jpeg;*.jpg;*.jpe)";
276 m_width = m_height = -1;
281 for( i = 0; i < 4; i++ )
283 m_td[i] = new short[max_dec_htable_size];
284 m_ta[i] = new short[max_dec_htable_size];
289 grfmt_jpeg_reader::~grfmt_jpeg_reader()
291 for( int i = 0; i < 4; i++ )
301 int grfmt_jpeg_reader::get_color()
303 return m_planes == 3;
306 void grfmt_jpeg_reader::close()
311 bool grfmt_jpeg_reader::read_header()
315 bool result = false, is_jfif = false, is_sof = false,
316 is_qt = false, is_ht = false, is_sos = false;
318 assert( strlen(m_filename) != 0 );
319 if( !m_strm.open( m_filename )) return false;
321 memset( m_is_tq, 0, sizeof(m_is_tq));
322 memset( m_is_td, 0, sizeof(m_is_td));
323 memset( m_is_ta, 0, sizeof(m_is_ta));
328 rm_byte_stream& lstrm = m_strm.m_low_strm;
330 lstrm.skeep( 2 ); // skip SOI marker
334 int marker = m_strm.find_marker() & 255;
336 // check for standalone markers
337 if( marker != 0xD8 /* SOI */ && marker != 0xD9 /* EOI */ &&
338 marker != 0x01 /* TEM */ && !( 0xD0 <= marker && marker <= 0xD7 ))
340 int pos = lstrm.get_pos();
341 int length = lstrm.get_word();
346 lstrm.get_bytes( buffer, 5 );
347 if( strcmp(buffer, "JFIF") == 0 ) // JFIF identification
349 m_version = lstrm.get_word();
355 m_precision = lstrm.get_byte();
356 m_height = lstrm.get_word();
357 m_width = lstrm.get_word();
358 m_planes = lstrm.get_byte();
360 if( m_width == 0 || m_height == 0 || // DNL not supported
361 (m_planes != 1 && m_planes != 3)) goto parsing_end;
363 memset( m_ci, -1, sizeof(m_ci));
365 for( i = 0; i < m_planes; i++ )
367 int idx = lstrm.get_byte();
369 if( idx < 1 || idx > m_planes ) // wrong index
373 cmp_info& ci = m_ci[idx-1];
375 if( ci.tq > 0 /* duplicated description */) goto parsing_end;
377 ci.h = (char)lstrm.get_byte();
378 ci.v = (char)(ci.h & 15);
380 ci.tq = (char)lstrm.get_byte();
381 if( !((ci.h == 1 || ci.h == 2 || ci.h == 4) &&
382 (ci.v == 1 || ci.v == 2 || ci.v == 4) &&
384 // chroma mcu-parts should have equal sizes and
385 // be non greater then luma sizes
386 !( i != 2 || (ci.h == m_ci[1].h && ci.v == m_ci[1].v &&
387 ci.h <= m_ci[0].h && ci.v <= m_ci[0].v)))
391 m_type = marker - 0xC0;
395 if( !load_quant_tables( length )) goto parsing_end;
400 if( !load_huffman_tables( length )) goto parsing_end;
410 m_MCUs = lstrm.get_word();
413 lstrm.set_pos( pos + length );
422 result = is_jfif && is_sof && is_qt && is_ht && is_sos;
425 m_width = m_height = -1;
433 bool grfmt_jpeg_reader::load_quant_tables( int length )
438 rm_byte_stream& lstrm = m_strm.m_low_strm;
443 int tq = lstrm.get_byte();
447 tq_size = (64<<size) + 1;
448 if( tq > 3 || size > 1 || length < tq_size ) return false;
451 lstrm.get_bytes( buffer, tq_size - 1 );
453 if( size == 0 ) // 8 bit quant factors
455 for( i = 0; i < 64; i++ )
458 m_tq[tq][idx] = buffer[i] * idct_prescale[idx];
461 else // 16 bit quant factors
463 for( i = 0; i < 64; i++ )
466 m_tq[tq][idx] = ((unsigned short*)buffer)[i] *
477 bool grfmt_jpeg_reader::load_huffman_tables( int length )
479 const int max_bits = 16;
484 rm_byte_stream& lstrm = m_strm.m_low_strm;
489 int t = lstrm.get_byte();
493 if( t > 3 || hclass > 1 || length < 17 ) return false;
496 lstrm.get_bytes( buffer, max_bits );
497 for( i = 0, ht_size = 0; i < max_bits; i++ ) ht_size += buffer[i];
499 if( length < ht_size ) return false;
502 lstrm.get_bytes( buffer + max_bits, ht_size );
504 if( !::bs_create_decode_huffman_table(
505 ::bs_create_source_huffman_table(
506 buffer, buffer2, max_bits, first_table_bits ),
507 hclass == 0 ? m_td[t] : m_ta[t],
508 max_dec_htable_size )) return false;
518 bool grfmt_jpeg_reader::read_data( byte* data, int pitch, int color )
520 if( m_offset < 0 || !m_strm.is_opened())
523 if( m_precision == 8 )
525 for( int i = 0; i < 4; i++ )
528 for( int j = 0; j < 64; j++ )
537 rm_byte_stream& lstrm = m_strm.m_low_strm;
538 lstrm.set_pos( m_offset );
542 int marker = m_strm.find_marker() & 255;
544 if( marker == 0xD8 /* SOI */ || marker == 0xD9 /* EOI */ )
547 // check for standalone markers
548 if( marker != 0x01 /* TEM */ && !( 0xD0 <= marker && marker <= 0xD7 ))
550 int pos = lstrm.get_pos();
551 int length = lstrm.get_word();
556 if( !load_huffman_tables( length )) goto decoding_end;
562 int idx[3] = { -1, -1, -1 };
563 int i, ns = lstrm.get_byte();
564 int sum = 0, a; // spectral selection & approximation
566 if( ns != m_planes ) goto decoding_end;
567 for( i = 0; i < ns; i++ )
569 int td, ta, c = lstrm.get_byte() - 1;
570 if( c < 0 || m_planes <= c )
575 if( idx[c] != -1 ) goto decoding_end;
577 td = lstrm.get_byte();
580 if( !(ta <= 3 && m_is_ta[ta] &&
581 td <= 3 && m_is_td[td] &&
582 m_is_tq[m_ci[c].tq]) )
585 m_ci[c].td = (char)td;
586 m_ci[c].ta = (char)ta;
588 sum += m_ci[c].h*m_ci[c].v;
591 if( sum > 10 ) goto decoding_end;
593 m_ss = lstrm.get_byte();
594 m_se = lstrm.get_byte();
596 a = lstrm.get_byte();
600 process_scan( idx, ns, data, pitch, color );
601 goto decoding_end; // only single scan case is supported now
604 //m_offset = pos - 2;
608 m_MCUs = lstrm.get_word();
612 if( marker != 0xDA ) lstrm.set_pos( pos + length );
624 void grfmt_jpeg_reader::reset_decoder()
626 m_ci[0].dc_pred = m_ci[1].dc_pred = m_ci[2].dc_pred = 0;
629 void grfmt_jpeg_reader::process_scan( int* idx, int ns, byte* data, int pitch, int color )
631 int i, s = 0, mcu, x1 = 0, y1 = 0;
634 int pos[3], h[3], v[3];
635 int x_shift = 0, y_shift = 0;
636 int nch = color ? 3 : 1;
638 assert( ns == m_planes && m_ss == 0 && m_se == 63 &&
639 m_al == 0 && m_ah == 0 ); // sequental & single scan
641 assert( idx[0] == 0 && (ns ==1 || (idx[1] == 1 && idx[2] == 2)));
643 for( i = 0; i < ns; i++ )
654 x_shift = h[0]/(h[1]*2);
655 y_shift = v[0]/(v[1]*2);
661 for( mcu = 0;; mcu++ )
667 if( mcu == m_MCUs && m_MCUs != 0 )
670 m_strm.align_on_byte();
675 for( i = 0; i < ns; i++ )
678 cmp = blocks[pos[c]];
679 for( y = 0; y < v[c]; y += 8, cmp += h[c]*8 )
680 for( x = 0; x < h[c]; x += 8 )
682 get_block( temp, c );
683 if( i < (color ? 3 : 1))
685 aan_idct8x8( temp, cmp + x, h[c] );
693 if( y1 + y2 > m_height ) y2 = m_height - y1;
694 if( x1 + x2 > m_width ) x2 = m_width - x1;
697 data1 = data + x1*nch;
700 for( y = 0; y < y2; y++, data1 += pitch, cmp += h[0] )
704 for( x = 0; x < x2; x++ )
706 int val = descale( cmp[x] + 128*4, 2 );
707 data1[x*3] = data1[x*3 + 1] = data1[x*3 + 2] = saturate( val );
712 for( x = 0; x < x2; x++ )
714 int val = descale( cmp[x] + 128*4, 2 );
715 data1[x] = saturate( val );
721 for( y = 0; y < y2; y++, data1 += pitch, cmp += h[0] )
725 int shift = h[1]*(y >> y_shift);
726 int* cmpCb = blocks[pos[1]] + shift;
727 int* cmpCr = blocks[pos[2]] + shift;
728 for( x = 0; x < x2; x++ )
730 int Y = (cmp[x] + 128*4) << fixc;
731 int Cb = cmpCb[x >> x_shift];
732 int Cr = cmpCr[x >> x_shift];
733 int t = (Y + Cb*b_cb) >> (fixc + 2);
734 data1[x*3] = saturate(t);
735 t = (Y + Cb*g_cb + Cr*g_cr) >> (fixc + 2);
736 data1[x*3 + 1] = saturate(t);
737 t = (Y + Cr*r_cr) >> (fixc + 2);
738 data1[x*3 + 2] = saturate(t);
743 for( x = 0; x < x2; x++ )
745 int val = descale( cmp[x] + 128*4, 2 );
746 data1[x] = saturate(val);
758 if( y1 >= m_height ) break;
764 void grfmt_jpeg_reader::get_block( int* block, int c )
766 memset( block, 0, 64*sizeof(block[0]) );
768 assert( 0 <= c && c < 3 );
769 const short* td = m_td[m_ci[c].td];
770 const short* ta = m_ta[m_ci[c].ta];
771 const int* tq = m_tq[m_ci[c].tq];
773 // get DC coefficient
774 int i = 0, cat = m_strm.get_huff( td );
775 int mask = bs_bits_masks[cat];
776 int val = m_strm.get( cat );
778 val -= (val*2 <= mask ? mask : 0);
779 m_ci[c].dc_pred = val += m_ci[c].dc_pred;
781 block[0] = (val * tq[0]) >> 16;
786 cat = m_strm.get_huff( ta );
787 if( cat == 0 ) break; // end of block
791 mask = bs_bits_masks[cat];
792 val = m_strm.get( cat );
794 val -= (val*2 <= mask ? mask : 0);
795 block[cat] = (val * tq[cat]) >> 16;