Update the changelog
[opencv] / otherlibs / VlGrFmts / rd_jpeg.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //  
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.    
8 //  
9 //    RCS:
10 //       Source:    rd_jpeg.cpp$
11 //       $Revision: 1.2 $
12 //      Purpose: 
13 //      Contents:
14 //      Authors: Vadim Pisarevsky
15 //  
16 //M*/
17
18 #include <assert.h>
19 #include <string.h>
20
21 #include "rd_jpeg.h"
22
23 //////////////////////  JPEG-oriented two-level bitstream ////////////////////////
24
25 rm_jpeg_bit_stream::rm_jpeg_bit_stream()
26 {
27 }
28
29 rm_jpeg_bit_stream::~rm_jpeg_bit_stream()
30 {
31 }
32
33
34 bool  rm_jpeg_bit_stream::open( const char* filename )
35 {
36     close();
37     allocate();
38     
39     m_is_opened = m_low_strm.open( filename );
40     if( m_is_opened ) set_pos(0);
41     return m_is_opened;
42 }
43
44
45 void  rm_jpeg_bit_stream::close()
46 {
47     m_low_strm.close();
48     m_is_opened = false;
49 }
50
51
52 void  rm_jpeg_bit_stream::read_block()
53 {
54     byte* end = m_start + m_block_size;
55     byte* current = m_start;
56
57     try
58     {
59         int sz = m_unget_size;
60         memmove( current - sz, m_end - sz, sz );
61         while( current < end )
62         {
63             int val = m_low_strm.get_byte();
64             if( val != 0xff )
65             {
66                 *current++ = (byte)val;
67             }
68             else
69             {
70                 val = m_low_strm.get_byte();
71                 if( val == 0 )
72                     *current++ = 0xFF;
73                 else if( !(0xD0 <= val && val <= 0xD7) )
74                 {
75                     m_low_strm.set_pos( m_low_strm.get_pos() - 2 );
76                     goto fetch_end;
77                 }
78             }
79         }
80 fetch_end: ;
81     }
82     catch( int )
83     {
84         if( current == m_start ) throw;       
85     }
86     m_current = m_start;
87     m_end = m_start + (((current - m_start) + 3) & -4);
88     bs_bswap_block( m_start, m_end );
89 }
90
91
92 void  rm_jpeg_bit_stream::flush()
93 {
94     m_end = m_start + m_block_size;
95     m_current = m_end - 4;
96     m_bit_idx = 0;
97 }
98
99 void  rm_jpeg_bit_stream::align_on_byte()
100 {
101     m_bit_idx &= -8;
102 }
103
104 int  rm_jpeg_bit_stream::find_marker()
105 {
106     int code = m_low_strm.get_word();
107     while( (code & 0xFF00) != 0xFF00 || (code == 0xFFFF || code == 0xFF00 ))
108     {
109         code = ((code&255) << 8) | m_low_strm.get_byte();
110     }
111     return code;
112 }
113
114
115 /****************************** JPEG (JFIF) reader ***************************/
116
117 // zigzag & IDCT prescaling (AAN algorithm) tables
118 static const byte zigzag[] =
119 {
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
125 };
126
127 const int idct_prescale[] =
128 {
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
137 };
138
139 #define fixb       8
140 #define fix(x, n)  (int)((x)*(1 << (n)) + .5)
141
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 )
146
147 #define fixc       10
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 )
152
153 // IDCT without prescaling
154 static void aan_idct8x8( int* src, int *dst, int pitch )
155 {
156     int   workspace[64], *work = workspace;
157     int   i;
158
159     /* Pass 1: process rows */
160     for( i = 8; i > 0; i--, src += 8, work += 8 )
161     {
162         /* Odd part */
163         int  x0 = src[5], x1 = src[3];
164         int  x2 = src[1], x3 = src[7];
165
166         int  x4 = x0 + x1; x0 -= x1;
167
168         x1 = x2 + x3; x2 -= x3;
169         x3 = x1 + x4; x1 -= x4;
170
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);
175
176         x0 -= x3;
177         x1 -= x0;
178         x2 += x1;
179
180         work[7] = x3; work[6] = x0;
181         work[5] = x1; work[4] = x2;
182
183         x2 = src[2]; x3 = src[6];
184         x0 = src[0]; x1 = src[4];
185
186         x4 = x2 + x3;
187         x2 = descale((x2-x3)*C1_414, fixb) - x4;
188
189         x3 = x0 + x1; x0 -= x1;
190         x1 = x3 + x4; x3 -= x4;
191         x4 = x0 + x2; x0 -= x2;
192
193         x2 = work[7];
194         x1 -= x2; x2 = 2*x2 + x1;
195         work[7] = x1; work[0] = x2;
196
197         x2 = work[6];
198         x1 = x4 + x2; x4 -= x2;
199         work[1] = x1; work[6] = x4;
200
201         x1 = work[5]; x2 = work[4];
202         x4 = x0 + x1; x0 -= x1;
203         x1 = x3 + x2; x3 -= x2;
204
205         work[2] = x4; work[5] = x0;
206         work[3] = x3; work[4] = x1;
207     }
208
209     /* Pass 2: process columns */
210     work = workspace;
211     for( i = 8; i > 0; i--, dst+=pitch, work++ )
212     {
213         /* Odd part */
214         int  x0 = work[8*5], x1 = work[8*3];
215         int  x2 = work[8*1], x3 = work[8*7];
216
217         int  x4 = x0 + x1; x0 -= x1;
218         x1 = x2 + x3; x2 -= x3;
219         x3 = x1 + x4; x1 -= x4;
220
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);
225
226         x0 -= x3;
227         x1 -= x0;
228         x2 += x1;
229
230         dst[7] = x3; dst[6] = x0;
231         dst[5] = x1; dst[4] = x2;
232
233         x2 = work[8*2]; x3 = work[8*6];
234         x0 = work[8*0]; x1 = work[8*4];
235
236         x4 = x2 + x3;
237         x2 = descale((x2-x3)*C1_414, fixb) - x4;
238
239         x3 = x0 + x1; x0 -= x1;
240         x1 = x3 + x4; x3 -= x4;
241         x4 = x0 + x2; x0 -= x2;
242
243         x2 = dst[7];
244         x1 -= x2; x2 = 2*x2 + x1;
245         x1 = descale(x1,3);
246         x2 = descale(x2,3);
247
248         dst[7] = x1; dst[0] = x2;
249
250         x2 = dst[6];
251         x1 = descale(x4 + x2,3);
252         x4 = descale(x4 - x2,3);
253         dst[1] = x1; dst[6] = x4;
254
255         x1 = dst[5]; x2 = dst[4];
256
257         x4 = descale(x0 + x1,3);
258         x0 = descale(x0 - x1,3);
259         x1 = descale(x3 + x2,3);
260         x3 = descale(x3 - x2,3);
261        
262         dst[2] = x4; dst[5] = x0;
263         dst[3] = x3; dst[4] = x1;
264     }
265 }
266
267
268 static const int max_dec_htable_size = 1 << 12;
269 static const int first_table_bits = 9;
270
271 grfmt_jpeg_reader::grfmt_jpeg_reader()
272 {
273     m_sign_len = 3;
274     m_signature ="\xFF\xD8\xFF";
275     m_description = "JFIF files (*.jpeg;*.jpg;*.jpe)";
276     m_width = m_height = -1;
277     m_planes= -1;
278     m_offset= -1;
279
280     int i;
281     for( i = 0; i < 4; i++ )
282     {
283         m_td[i] = new short[max_dec_htable_size];
284         m_ta[i] = new short[max_dec_htable_size];
285     }
286 }
287
288
289 grfmt_jpeg_reader::~grfmt_jpeg_reader()
290 {
291     for( int i = 0; i < 4; i++ )
292     {
293         delete m_td[i];
294         m_td[i] = 0;
295         delete m_ta[i];
296         m_ta[i] = 0;
297     }
298 }
299
300
301 int  grfmt_jpeg_reader::get_color()
302 {
303     return m_planes == 3;
304 }
305
306 void  grfmt_jpeg_reader::close()
307 {
308     m_strm.close();
309 }
310
311 bool grfmt_jpeg_reader::read_header()
312 {
313     char buffer[16];
314     int  i;
315     bool result = false, is_jfif = false, is_sof = false, 
316          is_qt = false, is_ht = false, is_sos = false;
317     
318     assert( strlen(m_filename) != 0 );
319     if( !m_strm.open( m_filename )) return false;
320
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));
324     m_MCUs = 0;
325
326     try
327     {
328         rm_byte_stream& lstrm = m_strm.m_low_strm;
329         
330         lstrm.skeep( 2 ); // skip SOI marker
331         
332         for(;;)
333         {
334             int marker = m_strm.find_marker() & 255;
335
336             // check for standalone markers
337             if( marker != 0xD8 /* SOI */ && marker != 0xD9 /* EOI */ &&
338                 marker != 0x01 /* TEM */ && !( 0xD0 <= marker && marker <= 0xD7 ))
339             {
340                 int pos    = lstrm.get_pos();
341                 int length = lstrm.get_word();
342             
343                 switch( marker )
344                 {
345                 case 0xE0: // APP0
346                     lstrm.get_bytes( buffer, 5 );
347                     if( strcmp(buffer, "JFIF") == 0 ) // JFIF identification
348                     {
349                         m_version = lstrm.get_word();
350                         is_jfif = true;
351                     }
352                     break;
353
354                 case 0xC0: // SOF0
355                     m_precision = lstrm.get_byte();
356                     m_height = lstrm.get_word();
357                     m_width = lstrm.get_word();
358                     m_planes = lstrm.get_byte();
359
360                     if( m_width == 0 || m_height == 0 || // DNL not supported
361                        (m_planes != 1 && m_planes != 3)) goto parsing_end;
362                 
363                     memset( m_ci, -1, sizeof(m_ci));
364
365                     for( i = 0; i < m_planes; i++ )
366                     {
367                         int idx = lstrm.get_byte();
368
369                         if( idx < 1 || idx > m_planes ) // wrong index
370                         {
371                             idx = i+1; // hack
372                         }
373                         cmp_info& ci = m_ci[idx-1];
374                         
375                         if( ci.tq > 0 /* duplicated description */) goto parsing_end;
376
377                         ci.h = (char)lstrm.get_byte();
378                         ci.v = (char)(ci.h & 15);
379                         ci.h >>= 4;
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) &&
383                               ci.tq < 3) ||
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)))
388                             goto parsing_end;
389                     }
390                     is_sof = true;
391                     m_type = marker - 0xC0;
392                     break;
393
394                 case 0xDB: // DQT
395                     if( !load_quant_tables( length )) goto parsing_end;
396                     is_qt = true;
397                     break;
398
399                 case 0xC4: // DHT
400                     if( !load_huffman_tables( length )) goto parsing_end;
401                     is_ht = true;
402                     break;
403
404                 case 0xDA: // SOS
405                     is_sos = true;
406                     m_offset = pos - 2;
407                     goto parsing_end;
408
409                 case 0xDD: // DRI
410                     m_MCUs = lstrm.get_word();
411                     break;
412                 }
413                 lstrm.set_pos( pos + length );
414             }
415         }
416 parsing_end: ;
417     }
418     catch( int )
419     {
420     }
421
422     result = is_jfif && is_sof && is_qt && is_ht && is_sos;
423     if( !result )
424     {
425         m_width = m_height = -1;
426         m_offset = -1;
427         m_strm.close();
428     }
429     return result;
430 }
431
432
433 bool grfmt_jpeg_reader::load_quant_tables( int length )
434 {
435     byte buffer[128];
436     int  i, tq_size;
437     
438     rm_byte_stream& lstrm = m_strm.m_low_strm;
439     length -= 2;
440
441     while( length > 0 )
442     {
443         int tq = lstrm.get_byte();
444         int size = tq >> 4;
445         tq &= 15;
446
447         tq_size = (64<<size) + 1; 
448         if( tq > 3 || size > 1 || length < tq_size ) return false;
449         length -= tq_size;
450
451         lstrm.get_bytes( buffer, tq_size - 1 );
452         
453         if( size == 0 ) // 8 bit quant factors
454         {
455             for( i = 0; i < 64; i++ )
456             {
457                 int idx = zigzag[i];
458                 m_tq[tq][idx] = buffer[i] * idct_prescale[idx];
459             }
460         }
461         else // 16 bit quant factors
462         {
463             for( i = 0; i < 64; i++ )
464             {
465                 int idx = zigzag[i];
466                 m_tq[tq][idx] = ((unsigned short*)buffer)[i] * 
467                                 idct_prescale[idx];
468             }
469         }
470         m_is_tq[tq] = true;
471     }
472
473     return true;
474 }
475
476
477 bool grfmt_jpeg_reader::load_huffman_tables( int length )
478 {
479     const int max_bits = 16;
480     byte buffer[1024];
481     int  buffer2[1024];
482
483     int  i, ht_size;
484     rm_byte_stream& lstrm = m_strm.m_low_strm;
485     length -= 2;
486
487     while( length > 0 )
488     {
489         int t = lstrm.get_byte();
490         int hclass = t >> 4;
491         t &= 15;
492
493         if( t > 3 || hclass > 1 || length < 17 ) return false;
494         length -= 17;
495
496         lstrm.get_bytes( buffer, max_bits );
497         for( i = 0, ht_size = 0; i < max_bits; i++ ) ht_size += buffer[i];
498
499         if( length < ht_size ) return false;
500         length -= ht_size;
501
502         lstrm.get_bytes( buffer + max_bits, ht_size );
503         
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;
509         if( hclass == 0 )
510             m_is_td[t] = true;
511         else
512             m_is_ta[t] = true;
513     }
514     return true;
515 }
516
517
518 bool grfmt_jpeg_reader::read_data( byte* data, int pitch, int color )
519 {
520     if( m_offset < 0 || !m_strm.is_opened())
521         return false;
522
523     if( m_precision == 8 )
524     {
525         for( int i = 0; i < 4; i++ )
526             if( m_is_tq[i] )
527             {
528                 for( int j = 0; j < 64; j++ )
529                 {
530                     m_tq[i][j] <<= 4;
531                 }
532             }
533     }
534
535     try
536     {
537         rm_byte_stream& lstrm = m_strm.m_low_strm;
538         lstrm.set_pos( m_offset );
539
540         for(;;)
541         {
542             int marker = m_strm.find_marker() & 255;
543
544             if( marker == 0xD8 /* SOI */ || marker == 0xD9 /* EOI */ )
545                 goto decoding_end;
546
547             // check for standalone markers
548             if( marker != 0x01 /* TEM */ && !( 0xD0 <= marker && marker <= 0xD7 ))
549             {
550                 int pos    = lstrm.get_pos();
551                 int length = lstrm.get_word();
552             
553                 switch( marker )
554                 {
555                 case 0xC4: // DHT
556                     if( !load_huffman_tables( length )) goto decoding_end;
557                     break;
558
559                 case 0xDA: // SOS
560                     // read scan header
561                     {
562                         int idx[3] = { -1, -1, -1 };
563                         int i, ns = lstrm.get_byte();
564                         int sum = 0, a; // spectral selection & approximation
565
566                         if( ns != m_planes ) goto decoding_end;
567                         for( i = 0; i < ns; i++ )
568                         {
569                             int td, ta, c = lstrm.get_byte() - 1;
570                             if( c < 0 || m_planes <= c )
571                             {
572                                 c = i; // hack
573                             }
574                             
575                             if( idx[c] != -1 ) goto decoding_end;
576                             idx[i] = c;
577                             td = lstrm.get_byte();
578                             ta = td & 15;
579                             td >>= 4;
580                             if( !(ta <= 3 && m_is_ta[ta] && 
581                                   td <= 3 && m_is_td[td] &&
582                                   m_is_tq[m_ci[c].tq]) )
583                                 goto decoding_end;
584
585                             m_ci[c].td = (char)td;
586                             m_ci[c].ta = (char)ta;
587
588                             sum += m_ci[c].h*m_ci[c].v;
589                         }
590
591                         if( sum > 10 ) goto decoding_end;
592
593                         m_ss = lstrm.get_byte();
594                         m_se = lstrm.get_byte();
595
596                         a = lstrm.get_byte();
597                         m_al = a & 15;
598                         m_ah = a >> 4;
599
600                         process_scan( idx, ns, data, pitch, color );
601                         goto decoding_end; // only single scan case is supported now
602                     }
603
604                     //m_offset = pos - 2;
605                     //break;
606
607                 case 0xDD: // DRI
608                     m_MCUs = lstrm.get_word();
609                     break;
610                 }
611                 
612                 if( marker != 0xDA ) lstrm.set_pos( pos + length );
613             }
614         }
615 decoding_end: ;
616     }
617     catch( int )
618     {
619     }
620     return true;
621 }
622
623
624 void  grfmt_jpeg_reader::reset_decoder()
625 {
626     m_ci[0].dc_pred = m_ci[1].dc_pred = m_ci[2].dc_pred = 0; 
627 }
628
629 void  grfmt_jpeg_reader::process_scan( int* idx, int ns, byte* data, int pitch, int color )
630 {
631     int   i, s = 0, mcu, x1 = 0, y1 = 0;
632     int   temp[64];
633     int   blocks[10][64];
634     int   pos[3], h[3], v[3];
635     int   x_shift = 0, y_shift = 0;
636     int   nch = color ? 3 : 1;
637
638     assert( ns == m_planes && m_ss == 0 && m_se == 63 &&
639             m_al == 0 && m_ah == 0 ); // sequental & single scan
640
641     assert( idx[0] == 0 && (ns ==1 || (idx[1] == 1 && idx[2] == 2)));
642
643     for( i = 0; i < ns; i++ )
644     {
645         int c = idx[i];
646         h[c] = m_ci[c].h*8;
647         v[c] = m_ci[c].v*8;
648         pos[c] = s >> 6;
649         s += h[c]*v[c];
650     }
651
652     if( ns == 3 )
653     {
654         x_shift = h[0]/(h[1]*2);
655         y_shift = v[0]/(v[1]*2);
656     }
657
658     m_strm.flush();
659     reset_decoder();
660
661     for( mcu = 0;; mcu++ )
662     {
663         int  x2, y2, x, y;
664         int* cmp;
665         byte* data1;
666         
667         if( mcu == m_MCUs && m_MCUs != 0 )
668         {
669             reset_decoder();
670             m_strm.align_on_byte();
671             mcu = 0;
672         }
673
674         // get mcu
675         for( i = 0; i < ns; i++ )
676         {
677             int  c = idx[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 )
681                 {
682                     get_block( temp, c );
683                     if( i < (color ? 3 : 1))
684                     {
685                         aan_idct8x8( temp, cmp + x, h[c] );
686                     }
687                 }
688         }
689
690         y2 = v[0];
691         x2 = h[0];
692
693         if( y1 + y2 > m_height ) y2 = m_height - y1;
694         if( x1 + x2 > m_width ) x2 = m_width - x1;
695
696         cmp = blocks[0];
697         data1 = data + x1*nch;
698
699         if( ns == 1 )
700             for( y = 0; y < y2; y++, data1 += pitch, cmp += h[0] )
701             {
702                 if( color )
703                 {
704                     for( x = 0; x < x2; x++ )
705                     {
706                         int val = descale( cmp[x] + 128*4, 2 );
707                         data1[x*3] = data1[x*3 + 1] = data1[x*3 + 2] = saturate( val );
708                     }
709                 }
710                 else
711                 {
712                     for( x = 0; x < x2; x++ )
713                     {
714                         int val = descale( cmp[x] + 128*4, 2 );
715                         data1[x] = saturate( val );
716                     }
717                 }
718             }
719         else
720         {
721             for( y = 0; y < y2; y++, data1 += pitch, cmp += h[0] )
722             {
723                 if( color )
724                 {
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++ )
729                     {
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);
739                     }
740                 }
741                 else
742                 {
743                     for( x = 0; x < x2; x++ )
744                     {
745                         int val = descale( cmp[x] + 128*4, 2 );
746                         data1[x] = saturate(val);
747                     }
748                 }
749             }
750         }
751
752         x1 += h[0];
753         if( x1 >= m_width )
754         {
755             x1 = 0;
756             y1 += v[0];
757             data += v[0]*pitch;
758             if( y1 >= m_height ) break;
759         }
760     }
761 }
762
763
764 void  grfmt_jpeg_reader::get_block( int* block, int c )
765 {
766     memset( block, 0, 64*sizeof(block[0]) );
767
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];
772
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 );
777
778     val -= (val*2 <= mask ? mask : 0);
779     m_ci[c].dc_pred = val += m_ci[c].dc_pred;
780     
781     block[0] = (val * tq[0]) >> 16;
782
783     // get AC coeffs
784     for(;;)
785     {
786         cat = m_strm.get_huff( ta );
787         if( cat == 0 ) break; // end of block
788
789         i += (cat >> 4) + 1;
790         cat &= 15;
791         mask = bs_bits_masks[cat];
792         val  = m_strm.get( cat );
793         cat  = zigzag[i];
794         val -= (val*2 <= mask ? mask : 0);
795         block[cat] = (val * tq[cat]) >> 16;
796         assert( i <= 63 );
797         if( i >= 63 ) break;
798     }
799 }