Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

block.c

00001 /*****************************************************************************
00002  * block.c: MPEG2 video transrating module
00003  *****************************************************************************
00004  * Copyright (C) 2003-2004 the VideoLAN team
00005  * Copyright (C) 2003 Antoine Missout
00006  * Copyright (C) 2000-2003 Michel Lespinasse <[email protected]>
00007  * Copyright (C) 1999-2000 Aaron Holtzman <[email protected]>
00008  * $Id: block.c 11664 2005-07-09 06:17:09Z courmisch $
00009  *
00010  * Authors: Christophe Massiot <[email protected]>
00011  *          Laurent Aimar <[email protected]>
00012  *          Antoine Missout
00013  *          Michel Lespinasse <[email protected]>
00014  *          Aaron Holtzman <[email protected]>
00015  *
00016  * This program is free software; you can redistribute it and/or modify
00017  * it under the terms of the GNU General Public License as published by
00018  * the Free Software Foundation; either version 2 of the License, or
00019  * (at your option) any later version.
00020  *
00021  * This program is distributed in the hope that it will be useful,
00022  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024  * GNU General Public License for more details.
00025  *
00026  * You should have received a copy of the GNU General Public License
00027  * along with this program; if not, write to the Free Software
00028  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00029  *****************************************************************************/
00030 
00031 /*****************************************************************************
00032  * Preamble
00033  *****************************************************************************/
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 #define NDEBUG 1
00037 #include <assert.h>
00038 #include <math.h>
00039 
00040 #include <vlc/vlc.h>
00041 #include <vlc/sout.h>
00042 #include <vlc/input.h>
00043 
00044 #include "transrate.h"
00045 
00046 /****************************************************************************
00047  * transrater, code from M2VRequantizer http://www.metakine.com/
00048  ****************************************************************************/
00049 
00051 
00052 #include "getvlc.h"
00053 #include "putvlc.h"
00054 
00055 static inline int saturate( int i_value )
00056 {
00057     if ( i_value > 2047 )
00058         return 2047;
00059     if ( i_value < -2048 )
00060         return -2048;
00061     return i_value;
00062 }
00063 
00064 static int64_t get_score( const RunLevel *blk, RunLevel *new_blk, int i_qscale, int i_qscale_new )
00065 {
00066     int64_t score = 0;
00067     int i1 = -1, i2 = -1;
00068 
00069     while ( new_blk->level )
00070     {
00071         int new_level = new_blk->level;
00072         int level = blk->level;
00073         if ( i1 > 64 || i2 > 64 || !blk->run || !new_blk->run ) return score;
00074         if ( i1 + blk->run == i2 + new_blk->run )
00075         {
00076             int64_t tmp = saturate(level * i_qscale) 
00077                             - saturate(new_level * i_qscale_new);
00078             i1 += blk->run;
00079             i2 += new_blk->run;
00080             score += tmp * tmp;
00081             blk++;
00082             new_blk++;
00083         }
00084         else
00085         {
00086             int64_t tmp = saturate(level * i_qscale);
00087             i1 += blk->run;
00088             score += tmp * tmp;
00089             blk++;
00090         }
00091     }
00092 
00093     while ( blk->level )
00094     {
00095         int level = blk->level;
00096         int64_t tmp = saturate(level * i_qscale);
00097         i1 += blk->run;
00098         score += tmp * tmp;
00099         blk++;
00100     }
00101 
00102     return score;
00103 }
00104 
00105 static void change_qscale( const RunLevel *blk, RunLevel *new_blk, int i_qscale, int i_qscale_new, int intra )
00106 {
00107     int i = 0, li = 0;
00108     int rounding;
00109     if ( intra )
00110         rounding = i_qscale_new / 3;
00111     else
00112         rounding = i_qscale_new / 6;
00113 
00114     while ( blk->level )
00115     {
00116         int level = blk->level > 0 ? blk->level : -blk->level;
00117         int new_level = saturate(level * i_qscale) / i_qscale_new;
00118         i += blk->run;
00119 
00120         if ( new_level )
00121         {
00122             new_blk->run = i - li;
00123             new_blk->level = blk->level > 0 ? new_level : -new_level;
00124             new_blk++;
00125             li = i;
00126         }
00127         blk++;
00128     }
00129     new_blk->level = 0;
00130 }
00131 
00132 static const uint8_t non_linear_mquant_table[32] =
00133 {
00134     0, 1, 2, 3, 4, 5, 6, 7,
00135     8,10,12,14,16,18,20,22,
00136     24,28,32,36,40,44,48,52,
00137     56,64,72,80,88,96,104,112
00138 };
00139 static const uint8_t map_non_linear_mquant[113] =
00140 {
00141     0,1,2,3,4,5,6,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,
00142     16,17,17,17,18,18,18,18,19,19,19,19,20,20,20,20,21,21,21,21,22,22,
00143     22,22,23,23,23,23,24,24,24,24,24,24,24,25,25,25,25,25,25,25,26,26,
00144     26,26,26,26,26,26,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,29,
00145     29,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,31,31,31,31,31
00146 };
00147 
00148 int scale_quant( transrate_t *tr, double qrate )
00149 {
00150     int i_quant = (int)floor( tr->quantizer_scale * qrate + 0.5 );
00151 
00152     if ( tr->q_scale_type )
00153     {
00154         if ( i_quant < 1 )
00155             i_quant = 1;
00156         if ( i_quant > 112 )
00157             i_quant = 112;
00158         i_quant = non_linear_mquant_table[map_non_linear_mquant[i_quant]];
00159     }
00160     else
00161     {
00162         if ( i_quant < 2 )
00163             i_quant = 2;
00164         if ( i_quant > 62 )
00165             i_quant = 62;
00166         i_quant = (i_quant / 2) * 2; // Must be *even*
00167     }
00168 
00169     return i_quant;
00170 }
00171 
00172 int increment_quant( transrate_t *tr, int i_quant )
00173 {
00174     if ( tr->q_scale_type )
00175     {
00176         assert( i_quant >= 1 && i_quant <= 112 );
00177         i_quant = map_non_linear_mquant[i_quant] + 1;
00178         if ( i_quant > 31 )
00179             i_quant = 31;
00180         i_quant = non_linear_mquant_table[i_quant];
00181     }
00182     else
00183     {
00184         assert(!(i_quant & 1));
00185         i_quant += 2;
00186         if ( i_quant > 62 )
00187             i_quant = 62;
00188     }
00189     return i_quant;
00190 }
00191 
00192 
00193 static int decrement_quant( transrate_t *tr, int i_quant )
00194 {
00195     if ( tr->q_scale_type )
00196     {
00197         assert( i_quant >= 1 && i_quant <= 112 );
00198         i_quant = map_non_linear_mquant[i_quant] - 1;
00199         if ( i_quant < 1 )
00200             i_quant = 1;
00201         i_quant = non_linear_mquant_table[i_quant];
00202     }
00203     else
00204     {
00205         assert(!(i_quant & 1));
00206         i_quant -= 2;
00207         if ( i_quant < 2 )
00208             i_quant = 2;
00209     }
00210     return i_quant;
00211 }
00212 
00213 static void quantize_block( transrate_t *tr, RunLevel *new_blk, int intra )
00214 {
00215     RunLevel old_blk[65];
00216     RunLevel *blk = old_blk;
00217     const uint8_t *old_matrix, *new_matrix;
00218     int i = 0, li = 0;
00219 
00220     memcpy( blk, new_blk, 65 * sizeof(RunLevel) );
00221     if ( intra )
00222     {
00223         old_matrix = tr->intra_quantizer_matrix;
00224         new_matrix = mpeg4_default_intra_matrix;
00225     }
00226     else
00227     {
00228         old_matrix = tr->non_intra_quantizer_matrix;
00229         new_matrix = mpeg4_default_non_intra_matrix;
00230     }
00231 
00232     while ( blk->level )
00233     {
00234         int level = blk->level > 0 ? blk->level : -blk->level;
00235         int new_level = (level * old_matrix[i] + new_matrix[i]/2)
00236                             / new_matrix[i];
00237         i += blk->run;
00238 
00239         if (new_level)
00240         {
00241             new_blk->run = i - li;
00242             new_blk->level = blk->level > 0 ? new_level : -new_level;
00243             new_blk++;
00244             li = i;
00245         }
00246         blk++;
00247     }
00248     new_blk->level = 0;
00249 }
00250 
00251 int transrate_mb( transrate_t *tr, RunLevel blk[6][65], RunLevel new_blk[6][65],
00252                   int i_cbp, int intra )
00253 {
00254     int i_qscale = tr->quantizer_scale;
00255     int i_guessed_qscale = tr->new_quantizer_scale;
00256     int64_t i_last_error = 0;
00257     int i_last_qscale;
00258     int i_last_qscale_same_error = 0;
00259     int i_direction = 0;
00260     int i_new_cbp;
00261     int i_nb_blocks = 0;
00262     int i_nb_coeffs = 0;
00263     int i;
00264 
00265     for ( i = 0; i < 6; i++ )
00266     {
00267         if ( i_cbp & (1 << (5 - i)) )
00268         {
00269             RunLevel *cur_blk = blk[i];
00270             i_nb_blocks++;
00271             while ( cur_blk->level )
00272             {
00273                 cur_blk++;
00274                 i_nb_coeffs++;
00275             }
00276         }
00277     }
00278 
00279     /* See if we can change quantizer scale */
00280     for ( ; ; )
00281     {
00282         int64_t i_error = 0;
00283         i_new_cbp = 0;
00284 
00285         for ( i = 0; i < 6; i++ )
00286         {
00287             if ( i_cbp & (1 << (5 - i)) )
00288             {
00289                 int64_t i_block_error;
00290                 change_qscale( blk[i], new_blk[i], i_qscale, i_guessed_qscale,
00291                                intra );
00292                 i_block_error = get_score( blk[i], new_blk[i],
00293                                            i_qscale, i_guessed_qscale );
00294                 if ( i > 3 ) i_block_error *= 4;
00295                 if ( i_block_error > i_error )
00296                     i_error = i_block_error;
00297                 if ( new_blk[i]->level )
00298                     i_new_cbp |= (1 << (5 - i));
00299             }
00300         }
00301 
00302         if ( i_error >= (int64_t)tr->i_minimum_error
00303                 && i_error <= (int64_t)tr->i_admissible_error )
00304         {
00305             break;
00306         }
00307         if ( i_nb_coeffs <= 15 && i_error <= (int64_t)tr->i_admissible_error )
00308         {
00309             /* There is no interest in changing the qscale (takes up 5 bits
00310              * we won't regain) */
00311             break;
00312         }
00313 
00314         if ( !i_direction )
00315         {
00316             if ( i_error > (int64_t)tr->i_admissible_error )
00317             {
00318                 i_direction = -1;
00319                 i_last_qscale = i_guessed_qscale;
00320                 i_guessed_qscale = decrement_quant( tr, i_guessed_qscale );
00321             }
00322             else
00323             {
00324                 i_direction = +1;
00325                 i_last_qscale = i_guessed_qscale;
00326                 i_guessed_qscale = increment_quant( tr, i_guessed_qscale );
00327                 i_last_error = i_error;
00328                 i_last_qscale_same_error = i_last_qscale;
00329             }
00330             if ( i_guessed_qscale == i_last_qscale )
00331                 break;
00332         }
00333         else if ( i_direction < 0 )
00334         {
00335             if ( i_error > (int64_t)tr->i_admissible_error )
00336             {
00337                 i_last_qscale = i_guessed_qscale;
00338                 i_guessed_qscale = decrement_quant( tr, i_guessed_qscale );
00339                 if ( i_guessed_qscale == i_last_qscale )
00340                     break;
00341             }
00342             else
00343             {
00344                 break;
00345             }
00346         }
00347         else
00348         {
00349             if ( i_error < (int64_t)tr->i_minimum_error )
00350             {
00351                 i_last_qscale = i_guessed_qscale;
00352                 i_guessed_qscale = increment_quant( tr, i_guessed_qscale );
00353                 if ( i_error > i_last_error )
00354                 {
00355                     i_last_error = i_error;
00356                     i_last_qscale_same_error = i_last_qscale;
00357                 }
00358                 if ( i_guessed_qscale == i_last_qscale )
00359                 {
00360                     if ( i_last_error == i_error )
00361                     {
00362                         i_guessed_qscale = i_last_qscale_same_error;
00363                         if ( i_guessed_qscale == i_qscale )
00364                         {
00365                             memcpy( new_blk, blk, sizeof(RunLevel)*65*6 );
00366                             i_new_cbp = i_cbp;
00367                         }
00368                         else
00369                         {
00370                             i_new_cbp = 0;
00371                             for ( i = 0; i < 6; i++ )
00372                             {
00373                                 if ( i_cbp & (1 << (5 - i)) )
00374                                 {
00375                                     change_qscale( blk[i], new_blk[i],
00376                                                    i_qscale, i_guessed_qscale,
00377                                                    intra );
00378                                     if ( new_blk[i]->level )
00379                                         i_new_cbp |= (1 << (5 - i));
00380                                 }
00381                             }
00382                         }
00383                     }
00384                     break;
00385                 }
00386             }
00387             else
00388             {
00389                 if ( i_error > (int64_t)tr->i_admissible_error
00390                         || i_last_error == i_error )
00391                 {
00392                     i_guessed_qscale = i_last_qscale_same_error;
00393                     if ( i_guessed_qscale == i_qscale )
00394                     {
00395                         memcpy( new_blk, blk, sizeof(RunLevel)*65*6 );
00396                         i_new_cbp = i_cbp;
00397                     }
00398                     else
00399                     {
00400                         i_new_cbp = 0;
00401                         for ( i = 0; i < 6; i++ )
00402                         {
00403                             if ( i_cbp & (1 << (5 - i)) )
00404                             {
00405                                 change_qscale( blk[i], new_blk[i],
00406                                                i_qscale, i_guessed_qscale,
00407                                                intra );
00408                                 if ( new_blk[i]->level )
00409                                     i_new_cbp |= (1 << (5 - i));
00410                             }
00411                         }
00412                     }
00413                 }
00414                 break;
00415             }
00416         }
00417     }
00418 
00419     tr->new_quantizer_scale = i_guessed_qscale;
00420 
00421 #if 0
00422     /* Now see if we can drop coeffs */
00423     for ( i = 0; i < 6; i++ )
00424     {
00425         if ( i_new_cbp & (1 << (5 - i)) )
00426         {
00427             for ( ; ; )
00428             {
00429                 RunLevel *last_blk = new_blk[i];
00430                 uint8_t old_level;
00431 
00432                 while ( last_blk[1].level )
00433                     last_blk++;
00434                 if ( last_blk == new_blk[i] )
00435                     break;
00436                 old_level = last_blk->level;
00437                 last_blk->level = 0;
00438                 i_error = get_score( blk[i], new_blk[i],
00439                                      i_qscale, i_guessed_qscale );
00440                 if ( i_error > tr->i_admissible_error )
00441                 {
00442                     last_blk->level = old_level;
00443                     break;
00444                 }
00445             }
00446         }
00447     }
00448 #endif
00449 
00450     return i_new_cbp;
00451 }
00452 
00453 void get_intra_block_B14( transrate_t *tr, RunLevel *blk )
00454 {
00455     bs_transrate_t *bs = &tr->bs;
00456     int i, li;
00457     int val;
00458     const DCTtab * tab;
00459 
00460     li = i = 0;
00461 
00462     for( ;; )
00463     {
00464         if (bs->i_bit_in_cache >= 0x28000000)
00465         {
00466             tab = DCT_B14AC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
00467 
00468             i += tab->run;
00469             if (i >= 64) break; /* end of block */
00470 
00471     normal_code:
00472             bs_flush( bs, tab->len );
00473             val = tab->level;
00474             val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
00475             blk->level = val;
00476             blk->run = i - li - 1;
00477             li = i;
00478             blk++;
00479 
00480             bs_flush( bs, 1 );
00481             continue;
00482         }
00483         else if (bs->i_bit_in_cache >= 0x04000000)
00484         {
00485             tab = DCT_B14_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
00486 
00487             i += tab->run;
00488             if (i < 64) goto normal_code;
00489 
00490             /* escape code */
00491             i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
00492             if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
00493 
00494             bs_flush( bs, 12 );
00495             val = SBITS (bs->i_bit_in_cache, 12);
00496             blk->level = val;
00497             blk->run = i - li - 1;
00498             li = i;
00499             blk++;
00500 
00501             bs_flush( bs, 12 );
00502 
00503             continue;
00504         }
00505         else if (bs->i_bit_in_cache >= 0x02000000)
00506         {
00507             tab = DCT_B14_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
00508             i += tab->run;
00509             if (i < 64 ) goto normal_code;
00510         }
00511         else if (bs->i_bit_in_cache >= 0x00800000)
00512         {
00513             tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
00514             i += tab->run;
00515             if (i < 64 ) goto normal_code;
00516         }
00517         else if (bs->i_bit_in_cache >= 0x00200000)
00518         {
00519             tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
00520             i += tab->run;
00521             if (i < 64 ) goto normal_code;
00522         }
00523         else
00524         {
00525             tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
00526             bs_flush( bs, 16 );
00527             i += tab->run;
00528             if (i < 64 ) goto normal_code;
00529         }
00530         fprintf(stderr, "Err in B14\n");
00531         tr->b_error = 1;
00532         break;  /* illegal, check needed to avoid buffer overflow */
00533     }
00534     bs_flush( bs, 2 );    /* dump end of block code */
00535     blk->level = 0;
00536 
00537     if ( tr->mpeg4_matrix )
00538         quantize_block( tr, blk, 1 );
00539 }
00540 
00541 void get_intra_block_B15( transrate_t *tr, RunLevel *blk )
00542 {
00543     bs_transrate_t *bs = &tr->bs;
00544     int i, li;
00545     int val;
00546     const DCTtab * tab;
00547 
00548     li = i = 0;
00549 
00550     for( ;; )
00551     {
00552         if (bs->i_bit_in_cache >= 0x04000000)
00553         {
00554             tab = DCT_B15_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
00555 
00556             i += tab->run;
00557             if (i < 64)
00558             {
00559     normal_code:
00560                 bs_flush( bs, tab->len );
00561 
00562                 val = tab->level;
00563                 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
00564                 blk->level = val;
00565                 blk->run = i - li - 1;
00566                 li = i;
00567                 blk++;
00568 
00569                 bs_flush( bs, 1 );
00570                 continue;
00571             }
00572             else
00573             {
00574                 i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
00575 
00576                 if (i >= 64) break; /* illegal, check against buffer overflow */
00577 
00578                 bs_flush( bs, 12 );
00579                 val = SBITS (bs->i_bit_in_cache, 12);
00580                 blk->level = val;
00581                 blk->run = i - li - 1;
00582                 li = i;
00583                 blk++;
00584 
00585                 bs_flush( bs, 12 );
00586                 continue;
00587             }
00588         }
00589         else if (bs->i_bit_in_cache >= 0x02000000)
00590         {
00591             tab = DCT_B15_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
00592             i += tab->run;
00593             if (i < 64) goto normal_code;
00594         }
00595         else if (bs->i_bit_in_cache >= 0x00800000)
00596         {
00597             tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
00598             i += tab->run;
00599             if (i < 64) goto normal_code;
00600         }
00601         else if (bs->i_bit_in_cache >= 0x00200000)
00602         {
00603             tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
00604             i += tab->run;
00605             if (i < 64) goto normal_code;
00606         }
00607         else
00608         {
00609             tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
00610             bs_flush( bs, 16 );
00611             i += tab->run;
00612             if (i < 64) goto normal_code;
00613         }
00614         fprintf(stderr, "Err in B15\n");
00615         tr->b_error = 1;
00616         break;  /* illegal, check needed to avoid buffer overflow */
00617     }
00618     bs_flush( bs, 4 );    /* dump end of block code */
00619     blk->level = 0;
00620 
00621     if ( tr->mpeg4_matrix )
00622         quantize_block( tr, blk, 1 );
00623 }
00624 
00625 
00626 int get_non_intra_block( transrate_t *tr, RunLevel *blk )
00627 {
00628     bs_transrate_t *bs = &tr->bs;
00629     int i, li;
00630     int val;
00631     const DCTtab * tab;
00632 
00633     li = i = -1;
00634 
00635     if (bs->i_bit_in_cache >= 0x28000000)
00636     {
00637         tab = DCT_B14DC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
00638         goto entry_1;
00639     }
00640     else goto entry_2;
00641 
00642     for( ;; )
00643     {
00644         if (bs->i_bit_in_cache >= 0x28000000)
00645         {
00646             tab = DCT_B14AC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
00647 
00648     entry_1:
00649             i += tab->run;
00650             if (i >= 64)
00651             break;  /* end of block */
00652 
00653     normal_code:
00654 
00655             bs_flush( bs, tab->len );
00656             val = tab->level;
00657             val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
00658             blk->level = val;
00659             blk->run = i - li - 1;
00660             li = i;
00661             blk++;
00662 
00663             //if ( ((val) && (tab->level < tst)) || ((!val) && (tab->level >= tst)) )
00664             //  LOGF("level: %i val: %i tst : %i q: %i nq : %i\n", tab->level, val, tst, q, nq);
00665 
00666             bs_flush( bs, 1 );
00667             continue;
00668         }
00669 
00670     entry_2:
00671         if (bs->i_bit_in_cache >= 0x04000000)
00672         {
00673             tab = DCT_B14_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
00674 
00675             i += tab->run;
00676             if (i < 64) goto normal_code;
00677 
00678             /* escape code */
00679 
00680             i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
00681 
00682             if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
00683 
00684             bs_flush( bs, 12 );
00685             val = SBITS (bs->i_bit_in_cache, 12);
00686             blk->level = val;
00687             blk->run = i - li - 1;
00688             li = i;
00689             blk++;
00690 
00691             bs_flush( bs, 12 );
00692             continue;
00693         }
00694         else if (bs->i_bit_in_cache >= 0x02000000)
00695         {
00696             tab = DCT_B14_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
00697             i += tab->run;
00698             if (i < 64) goto normal_code;
00699         }
00700         else if (bs->i_bit_in_cache >= 0x00800000)
00701         {
00702             tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
00703             i += tab->run;
00704             if (i < 64) goto normal_code;
00705         }
00706         else if (bs->i_bit_in_cache >= 0x00200000)
00707         {
00708             tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
00709             i += tab->run;
00710             if (i < 64) goto normal_code;
00711         }
00712         else
00713         {
00714             tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
00715             bs_flush( bs, 16 );
00716 
00717             i += tab->run;
00718             if (i < 64) goto normal_code;
00719         }
00720         fprintf(stderr, "Err in non-intra\n");
00721         tr->b_error = 1;
00722         break;  /* illegal, check needed to avoid buffer overflow */
00723     }
00724     bs_flush( bs, 2 );    /* dump end of block code */
00725     blk->level = 0;
00726 
00727     if ( tr->mpeg4_matrix )
00728         quantize_block( tr, blk, 0 );
00729 
00730     return i;
00731 }
00732 
00733 static inline void putAC( bs_transrate_t *bs, int run, int signed_level, int vlcformat)
00734 {
00735     int level, len;
00736     const VLCtable *ptab = NULL;
00737 
00738     level = (signed_level<0) ? -signed_level : signed_level; /* abs(signed_level) */
00739 
00740     assert(!(run<0 || run>63 || level==0 || level>2047));
00741 
00742     len = 0;
00743 
00744     if (run<2 && level<41)
00745     {
00746         if (vlcformat)  ptab = &dct_code_tab1a[run][level-1];
00747         else ptab = &dct_code_tab1[run][level-1];
00748         len = ptab->len;
00749     }
00750     else if (run<32 && level<6)
00751     {
00752         if (vlcformat) ptab = &dct_code_tab2a[run-2][level-1];
00753         else ptab = &dct_code_tab2[run-2][level-1];
00754         len = ptab->len;
00755     }
00756 
00757     if (len) /* a VLC code exists */
00758     {
00759         bs_write( bs, ptab->code, len);
00760         bs_write( bs, signed_level<0, 1); /* sign */
00761     }
00762     else
00763     {
00764         bs_write( bs, 1l, 6); /* Escape */
00765         bs_write( bs, run, 6); /* 6 bit code for run */
00766         bs_write( bs, ((unsigned int)signed_level) & 0xFFF, 12);
00767     }
00768 }
00769 
00770 
00771 static inline void putACfirst( bs_transrate_t *bs, int run, int val)
00772 {
00773     if (run==0 && (val==1 || val==-1)) bs_write( bs, 2|(val<0), 2 );
00774     else putAC( bs, run, val, 0);
00775 }
00776 
00777 void putnonintrablk( bs_transrate_t *bs, RunLevel *blk)
00778 {
00779     assert(blk->level);
00780 
00781     putACfirst( bs, blk->run, blk->level );
00782     blk++;
00783 
00784     while (blk->level)
00785     {
00786         putAC( bs, blk->run, blk->level, 0 );
00787         blk++;
00788     }
00789 
00790     bs_write( bs, 2, 2 );
00791 }
00792 
00793 void putintrablk( bs_transrate_t *bs, RunLevel *blk, int vlcformat)
00794 {
00795     while (blk->level)
00796     {
00797         putAC( bs, blk->run, blk->level, vlcformat );
00798         blk++;
00799     }
00800 
00801     if (vlcformat)
00802         bs_write( bs, 6, 4 );
00803     else
00804         bs_write( bs, 2, 2 );
00805 }
00806 

Generated on Tue Dec 20 10:14:50 2005 for vlc-0.8.4a by  doxygen 1.4.2