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

frame.c

00001 /*****************************************************************************
00002  * frame.c: MPEG2 video transrating module
00003  *****************************************************************************
00004  * Copyright (C) 2003-2004 the VideoLAN team
00005  * Copyright (C) 2003 Antoine Missout <[email protected]>
00006  * Copyright (C) 2000-2003 Michel Lespinasse <[email protected]>
00007  * Copyright (C) 1999-2000 Aaron Holtzman <[email protected]>
00008  * $Id: frame.c 11664 2005-07-09 06:17:09Z courmisch $
00009  *
00010  * Authors: Christophe Massiot <[email protected]>
00011  *          Laurent Aimar <[email protected]>
00012  *          Antoine Missout <[email protected]>
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 
00050 // useful constants
00051 enum
00052 {
00053     I_TYPE = 1,
00054     P_TYPE = 2,
00055     B_TYPE = 3
00056 };
00057 
00058 
00060 
00061 #include "putvlc.h"
00062 
00063 #include "getvlc.h"
00064 
00065 static const int non_linear_quantizer_scale [] =
00066 {
00067      0,  1,  2,  3,  4,  5,   6,   7,
00068      8, 10, 12, 14, 16, 18,  20,  22,
00069     24, 28, 32, 36, 40, 44,  48,  52,
00070     56, 64, 72, 80, 88, 96, 104, 112
00071 };
00072 
00073 static inline int get_macroblock_modes( transrate_t *tr )
00074 {
00075     bs_transrate_t *bs = &tr->bs;
00076 
00077     int macroblock_modes;
00078     const MBtab * tab;
00079 
00080     switch( tr->picture_coding_type )
00081     {
00082         case I_TYPE:
00083 
00084             tab = MB_I + UBITS (bs->i_bit_in_cache, 1);
00085             bs_flush( bs, tab->len );
00086             macroblock_modes = tab->modes;
00087 
00088             if ((!(tr->frame_pred_frame_dct)) && (tr->picture_structure == FRAME_PICTURE))
00089             {
00090                 macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
00091                 bs_flush( bs, 1 );
00092             }
00093 
00094             return macroblock_modes;
00095 
00096         case P_TYPE:
00097 
00098             tab = MB_P + UBITS (bs->i_bit_in_cache, 5);
00099             bs_flush( bs, tab->len );
00100             macroblock_modes = tab->modes;
00101 
00102             if (tr->picture_structure != FRAME_PICTURE)
00103             {
00104                 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
00105                 {
00106                     macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
00107                     bs_flush( bs, 2 );
00108                 }
00109                 return macroblock_modes;
00110             }
00111             else if (tr->frame_pred_frame_dct)
00112             {
00113                 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
00114                     macroblock_modes |= MC_FRAME;
00115                 return macroblock_modes;
00116             }
00117             else
00118             {
00119                 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
00120                 {
00121                     macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
00122                     bs_flush( bs, 2 );
00123                 }
00124                 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
00125                 {
00126                     macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
00127                     bs_flush( bs, 1 );
00128                 }
00129                 return macroblock_modes;
00130             }
00131 
00132         case B_TYPE:
00133 
00134             tab = MB_B + UBITS (bs->i_bit_in_cache, 6);
00135             bs_flush( bs, tab->len );
00136             macroblock_modes = tab->modes;
00137 
00138             if( tr->picture_structure != FRAME_PICTURE)
00139             {
00140                 if (! (macroblock_modes & MACROBLOCK_INTRA))
00141                 {
00142                     macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
00143                     bs_flush( bs, 2 );
00144                 }
00145                 return macroblock_modes;
00146             }
00147             else if (tr->frame_pred_frame_dct)
00148             {
00149                 /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
00150                 macroblock_modes |= MC_FRAME;
00151                 return macroblock_modes;
00152             }
00153             else
00154             {
00155                 if (macroblock_modes & MACROBLOCK_INTRA) goto intra;
00156                 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
00157                 bs_flush( bs, 2 );
00158                 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
00159                 {
00160                     intra:
00161                     macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
00162                     bs_flush( bs, 1 );
00163                 }
00164                 return macroblock_modes;
00165             }
00166 
00167         default:
00168             return 0;
00169     }
00170 
00171 }
00172 
00173 static inline int get_quantizer_scale( transrate_t *tr )
00174 {
00175     bs_transrate_t *bs = &tr->bs;
00176 
00177     int quantizer_scale_code;
00178 
00179     quantizer_scale_code = UBITS (bs->i_bit_in_cache, 5);
00180     bs_flush( bs, 5 );
00181 
00182     if( tr->q_scale_type )
00183         return non_linear_quantizer_scale[quantizer_scale_code];
00184     else
00185         return quantizer_scale_code << 1;
00186 }
00187 
00188 static inline int get_motion_delta( bs_transrate_t *bs, const int f_code )
00189 {
00190     int delta;
00191     int sign;
00192     const MVtab * tab;
00193 
00194     if (bs->i_bit_in_cache & 0x80000000)
00195     {
00196         bs_copy( bs, 1 );
00197         return 0;
00198     }
00199     else if (bs->i_bit_in_cache >= 0x0c000000)
00200     {
00201 
00202         tab = MV_4 + UBITS (bs->i_bit_in_cache, 4);
00203         delta = (tab->delta << f_code) + 1;
00204         bs_copy( bs, tab->len);
00205 
00206         sign = SBITS (bs->i_bit_in_cache, 1);
00207         bs_copy( bs, 1 );
00208 
00209         if (f_code)
00210         {
00211             delta += UBITS (bs->i_bit_in_cache, f_code);
00212             bs_copy( bs, f_code);
00213         }
00214 
00215         return (delta ^ sign) - sign;
00216     }
00217     else
00218     {
00219 
00220         tab = MV_10 + UBITS (bs->i_bit_in_cache, 10);
00221         delta = (tab->delta << f_code) + 1;
00222         bs_copy( bs, tab->len);
00223 
00224         sign = SBITS (bs->i_bit_in_cache, 1);
00225         bs_copy( bs, 1);
00226 
00227         if (f_code)
00228         {
00229             delta += UBITS (bs->i_bit_in_cache, f_code);
00230             bs_copy( bs, f_code);
00231         }
00232 
00233         return (delta ^ sign) - sign;
00234     }
00235 }
00236 
00237 
00238 static inline int get_dmv( bs_transrate_t *bs )
00239 {
00240     const DMVtab * tab;
00241 
00242     tab = DMV_2 + UBITS (bs->i_bit_in_cache, 2);
00243     bs_copy( bs, tab->len);
00244     return tab->dmv;
00245 }
00246 
00247 static inline int get_coded_block_pattern( bs_transrate_t *bs )
00248 {
00249     const CBPtab * tab;
00250 
00251     if (bs->i_bit_in_cache >= 0x20000000)
00252     {
00253         tab = CBP_7 + (UBITS (bs->i_bit_in_cache, 7) - 16);
00254         bs_flush( bs, tab->len );
00255         return tab->cbp;
00256     }
00257     else
00258     {
00259         tab = CBP_9 + UBITS (bs->i_bit_in_cache, 9);
00260         bs_flush( bs, tab->len );
00261         return tab->cbp;
00262     }
00263 }
00264 
00265 static inline int get_luma_dc_dct_diff( bs_transrate_t *bs, uint32_t *bits, uint8_t *len )
00266 {
00267     const DCtab * tab;
00268     int size;
00269     int dc_diff;
00270 
00271     if (bs->i_bit_in_cache < 0xf8000000)
00272     {
00273         tab = DC_lum_5 + UBITS (bs->i_bit_in_cache, 5);
00274         size = tab->size;
00275         if (size)
00276         {
00277             *bits = bs_read( bs, tab->len );
00278             *len = tab->len;
00279             //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
00280             dc_diff = UBITS (bs->i_bit_in_cache, size);
00281             if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
00282             *bits <<= size;
00283             *bits |= bs_read( bs, size );
00284             *len += size;
00285             return dc_diff;
00286         }
00287         else
00288         {
00289             *bits = bs_read( bs, 3 );
00290             *len = 3;
00291             return 0;
00292         }
00293     }
00294     else
00295     {
00296         tab = DC_long + (UBITS (bs->i_bit_in_cache, 9) - 0x1e0);
00297         size = tab->size;
00298         *bits = bs_read( bs, tab->len );
00299         *len = tab->len;
00300         //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
00301         dc_diff = UBITS (bs->i_bit_in_cache, size);
00302         if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
00303         *bits <<= size;
00304         *bits |= bs_read( bs, size );
00305         *len += size;
00306         return dc_diff;
00307     }
00308 }
00309 
00310 static inline int get_chroma_dc_dct_diff( bs_transrate_t *bs, uint32_t *bits, uint8_t *len )
00311 {
00312     const DCtab * tab;
00313     int size;
00314     int dc_diff;
00315 
00316     if (bs->i_bit_in_cache < 0xf8000000)
00317     {
00318         tab = DC_chrom_5 + UBITS (bs->i_bit_in_cache, 5);
00319         size = tab->size;
00320         if (size)
00321         {
00322             *bits = bs_read( bs, tab->len );
00323             *len = tab->len;
00324             //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
00325             dc_diff = UBITS (bs->i_bit_in_cache, size);
00326             if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
00327             *bits <<= size;
00328             *bits |= bs_read( bs, size );
00329             *len += size;
00330             return dc_diff;
00331         }
00332         else
00333         {
00334             *bits = bs_read( bs, 2 );
00335             *len = 2;
00336             return 0;
00337         }
00338     }
00339     else
00340     {
00341         tab = DC_long + (UBITS (bs->i_bit_in_cache, 10) - 0x3e0);
00342         size = tab->size;
00343         *bits = bs_read( bs, tab->len + 1 );
00344         *len = tab->len + 1;
00345         //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
00346         dc_diff = UBITS (bs->i_bit_in_cache, size);
00347         if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
00348         *bits <<= size;
00349         *bits |= bs_read( bs, size );
00350         *len += size;
00351         return dc_diff;
00352     }
00353 }
00354 
00355 static void motion_fr_frame( bs_transrate_t *bs, unsigned int f_code[2] )
00356 {
00357     get_motion_delta( bs, f_code[0] );
00358     get_motion_delta( bs, f_code[1] );
00359 }
00360 
00361 static void motion_fr_field( bs_transrate_t *bs, unsigned int f_code[2] )
00362 {
00363     bs_copy( bs, 1);
00364 
00365     get_motion_delta( bs, f_code[0]);
00366     get_motion_delta( bs, f_code[1]);
00367 
00368     bs_copy( bs, 1);
00369 
00370     get_motion_delta( bs, f_code[0]);
00371     get_motion_delta( bs, f_code[1]);
00372 }
00373 
00374 static void motion_fr_dmv( bs_transrate_t *bs, unsigned int f_code[2] )
00375 {
00376     get_motion_delta( bs, f_code[0]);
00377     get_dmv( bs );
00378 
00379     get_motion_delta( bs, f_code[1]);
00380     get_dmv( bs );
00381 }
00382 
00383 static void motion_fi_field( bs_transrate_t *bs, unsigned int f_code[2] )
00384 {
00385     bs_copy( bs, 1);
00386 
00387     get_motion_delta( bs, f_code[0]);
00388     get_motion_delta( bs, f_code[1]);
00389 }
00390 
00391 static void motion_fi_16x8( bs_transrate_t *bs, unsigned int f_code[2] )
00392 {
00393     bs_copy( bs, 1);
00394 
00395     get_motion_delta( bs, f_code[0]);
00396     get_motion_delta( bs, f_code[1]);
00397 
00398     bs_copy( bs, 1);
00399 
00400     get_motion_delta( bs, f_code[0]);
00401     get_motion_delta( bs, f_code[1]);
00402 }
00403 
00404 static void motion_fi_dmv( bs_transrate_t *bs, unsigned int f_code[2] )
00405 {
00406     get_motion_delta( bs, f_code[0]);
00407     get_dmv( bs );
00408 
00409     get_motion_delta( bs, f_code[1]);
00410     get_dmv( bs );
00411 }
00412 
00413 
00414 #define MOTION_CALL(routine,direction)                      \
00415 do {                                                        \
00416     if ((direction) & MACROBLOCK_MOTION_FORWARD)            \
00417         routine( bs, tr->f_code[0]);                        \
00418     if ((direction) & MACROBLOCK_MOTION_BACKWARD)           \
00419         routine( bs, tr->f_code[1]);                        \
00420 } while (0)
00421 
00422 #define NEXT_MACROBLOCK                                         \
00423 do {                                                            \
00424     tr->h_offset += 16;                                         \
00425     if( tr->h_offset == tr->horizontal_size_value)              \
00426     {                                                           \
00427         tr->v_offset += 16;                                         \
00428         if (tr->v_offset > (tr->vertical_size_value - 16)) return;      \
00429         tr->h_offset = 0;                                       \
00430     }                                                           \
00431 } while (0)
00432 
00433 static void putmbdata( transrate_t *tr, int macroblock_modes )
00434 {
00435     bs_transrate_t *bs = &tr->bs;
00436 
00437     bs_write( bs,
00438               mbtypetab[tr->picture_coding_type-1][macroblock_modes&0x1F].code,
00439               mbtypetab[tr->picture_coding_type-1][macroblock_modes&0x1F].len);
00440 
00441     switch ( tr->picture_coding_type )
00442     {
00443         case I_TYPE:
00444             if ((! (tr->frame_pred_frame_dct)) && (tr->picture_structure == FRAME_PICTURE))
00445                 bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
00446             break;
00447 
00448         case P_TYPE:
00449             if (tr->picture_structure != FRAME_PICTURE)
00450             {
00451                 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
00452                     bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
00453                 break;
00454             }
00455             else if (tr->frame_pred_frame_dct) break;
00456             else
00457             {
00458                 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
00459                     bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
00460                 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
00461                     bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
00462                 break;
00463             }
00464 
00465         case B_TYPE:
00466             if (tr->picture_structure != FRAME_PICTURE)
00467             {
00468                 if (! (macroblock_modes & MACROBLOCK_INTRA))
00469                     bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
00470                 break;
00471             }
00472             else if (tr->frame_pred_frame_dct) break;
00473             else
00474             {
00475                 if (macroblock_modes & MACROBLOCK_INTRA) goto intra;
00476                 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
00477                 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
00478                 {
00479                     intra:
00480                     bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
00481                 }
00482                 break;
00483             }
00484     }
00485 }
00486 
00487 static const uint8_t map_non_linear_mquant[113] =
00488 {
00489     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,
00490     16,17,17,17,18,18,18,18,19,19,19,19,20,20,20,20,21,21,21,21,22,22,
00491     22,22,23,23,23,23,24,24,24,24,24,24,24,25,25,25,25,25,25,25,26,26,
00492     26,26,26,26,26,26,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,29,
00493     29,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,31,31,31,31,31
00494 };
00495 static inline void put_quantiser( transrate_t *tr )
00496 {
00497     bs_transrate_t *bs = &tr->bs;
00498 
00499     bs_write( bs, tr->q_scale_type ? map_non_linear_mquant[tr->new_quantizer_scale] : tr->new_quantizer_scale >> 1, 5 );
00500     tr->last_coded_scale = tr->new_quantizer_scale;
00501 }
00502 
00503 /* generate variable length code for macroblock_address_increment (6.3.16) */
00504 static inline void putaddrinc( transrate_t *tr, int addrinc )
00505 {
00506     bs_transrate_t *bs = &tr->bs;
00507 
00508     while ( addrinc >= 33 )
00509     {
00510         bs_write( bs, 0x08, 11 ); /* macroblock_escape */
00511         addrinc -= 33;
00512     }
00513 
00514     bs_write( bs, addrinctab[addrinc].code, addrinctab[addrinc].len );
00515 }
00516 
00517 static int slice_init( transrate_t *tr,  int code )
00518 {
00519     bs_transrate_t *bs = &tr->bs;
00520     int offset;
00521     const MBAtab * mba;
00522 
00523     tr->v_offset = (code - 1) * 16;
00524 
00525     tr->quantizer_scale = get_quantizer_scale( tr );
00526     if ( tr->new_quantizer_scale < tr->quantizer_scale )
00527         tr->new_quantizer_scale = scale_quant( tr, tr->qrate );
00528 
00529     /*LOGF("************************\nstart of slice %i in %s picture. ori quant: %i new quant: %i\n", code,
00530         (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
00531         quantizer_scale, new_quantizer_scale);*/
00532 
00533     /* ignore intra_slice and all the extra data */
00534     while (bs->i_bit_in_cache & 0x80000000)
00535     {
00536         bs_flush( bs, 9 );
00537     }
00538 
00539     /* decode initial macroblock address increment */
00540     offset = 0;
00541     for( ;; )
00542     {
00543         if (bs->i_bit_in_cache >= 0x08000000)
00544         {
00545             mba = MBA_5 + (UBITS (bs->i_bit_in_cache, 6) - 2);
00546             break;
00547         }
00548         else if (bs->i_bit_in_cache >= 0x01800000)
00549         {
00550             mba = MBA_11 + (UBITS (bs->i_bit_in_cache, 12) - 24);
00551             break;
00552         }
00553         else if( UBITS (bs->i_bit_in_cache, 12 ) == 8 )
00554         {
00555             /* macroblock_escape */
00556             offset += 33;
00557             bs_flush(bs, 11);
00558         }
00559         else
00560         {
00561             return -1;
00562         }
00563     }
00564 
00565     bs_flush(bs, mba->len + 1);
00566     tr->h_offset = (offset + mba->mba) << 4;
00567 
00568     while( tr->h_offset - (int)tr->horizontal_size_value >= 0)
00569     {
00570         tr->h_offset -= tr->horizontal_size_value;
00571         tr->v_offset += 16;
00572     }
00573 
00574     if( tr->v_offset > tr->vertical_size_value - 16 )
00575     {
00576         return -1;
00577     }
00578     return (offset + mba->mba);
00579 }
00580 
00581 static void mpeg2_slice( transrate_t *tr, const int code )
00582 {
00583     bs_transrate_t *bs = &tr->bs;
00584     int mba_inc;
00585     int first_in_slice = 1;
00586 
00587     if( (mba_inc = slice_init( tr, code )) < 0 )
00588     {
00589         return;
00590     }
00591 
00592     for( ;; )
00593     {
00594         const MBAtab * mba;
00595         int macroblock_modes;
00596         int mba_local;
00597         int i;
00598 
00599         while (unlikely(bs->i_bit_in < 24)) bs_refill( bs );
00600 
00601         macroblock_modes = get_macroblock_modes( tr );
00602         if (macroblock_modes & MACROBLOCK_QUANT)
00603             tr->quantizer_scale = get_quantizer_scale( tr );
00604         if (tr->new_quantizer_scale < tr->quantizer_scale)
00605             tr->new_quantizer_scale = scale_quant( tr, tr->qrate );
00606 
00607         //LOGF("blk %i : ", h_offset >> 4);
00608 
00609         if (macroblock_modes & MACROBLOCK_INTRA)
00610         {
00611             RunLevel block[6][65]; // terminated by level = 0, so we need 64+1
00612             RunLevel new_block[6][65]; // terminated by level = 0, so we need 64+1
00613             uint32_t dc[6];
00614             uint8_t  dc_len[6];
00615 
00616             // begin saving data
00617             int batb;
00618             uint8_t   p_n_ow[32], *p_n_w,
00619                     *p_o_ow = bs->p_ow, *p_o_w = bs->p_w;
00620             uint32_t  i_n_bit_out, i_n_bit_out_cache,
00621                     i_o_bit_out  = bs->i_bit_out, i_o_bit_out_cache = bs->i_bit_out_cache;
00622 
00623             bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
00624             bs->p_ow = bs->p_w = p_n_ow;
00625 
00626             //LOG("intra "); if (macroblock_modes & MACROBLOCK_QUANT) LOGF("got new quant: %i ", quantizer_scale);
00627 
00628             if (tr->concealment_motion_vectors)
00629             {
00630                 if (tr->picture_structure != FRAME_PICTURE)
00631                 {
00632                     bs_copy(bs, 1); /* remove field_select */
00633                 }
00634                 /* like motion_frame, but parsing without actual motion compensation */
00635                 get_motion_delta(bs, tr->f_code[0][0]);
00636                 get_motion_delta(bs, tr->f_code[0][1]);
00637 
00638                 bs_copy(bs, 1); /* remove marker_bit */
00639             }
00640 
00641             assert(bs->p_w - bs->p_ow < 32);
00642 
00643             p_n_w = bs->p_w;
00644             i_n_bit_out = bs->i_bit_out;
00645             i_n_bit_out_cache = bs->i_bit_out_cache;
00646             assert(bs->p_ow == p_n_ow);
00647 
00648             bs->i_bit_out = i_o_bit_out ;
00649             bs->i_bit_out_cache = i_o_bit_out_cache;
00650             bs->p_ow = p_o_ow;
00651             bs->p_w = p_o_w;
00652             // end saving data
00653 
00654             if( tr->intra_vlc_format )
00655             {
00656                 /* Luma */
00657                 for ( i = 0; i < 4; i++ )
00658                 {
00659                     get_luma_dc_dct_diff( bs, dc + i, dc_len + i );
00660                     get_intra_block_B15( tr, block[i] );
00661                     if (tr->b_error) return;
00662                 }
00663                 /* Chroma */
00664                 for ( ; i < 6; i++ )
00665                 {
00666                     get_chroma_dc_dct_diff( bs, dc + i, dc_len + i );
00667                     get_intra_block_B15( tr, block[i] );
00668                     if (tr->b_error) return;
00669                 }
00670             }
00671             else
00672             {
00673                 /* Luma */
00674                 for ( i = 0; i < 4; i++ )
00675                 {
00676                     get_luma_dc_dct_diff( bs, dc + i, dc_len + i );
00677                     get_intra_block_B14( tr, block[i] );
00678                     if (tr->b_error) return;
00679                 }
00680                 /* Chroma */
00681                 for ( ; i < 6; i++ )
00682                 {
00683                     get_chroma_dc_dct_diff( bs, dc + i, dc_len + i );
00684                     get_intra_block_B14( tr, block[i] );
00685                     if (tr->b_error) return;
00686                 }
00687             }
00688 
00689             transrate_mb( tr, block, new_block, 0x3f, 1 );
00690 
00691             if (tr->last_coded_scale == tr->new_quantizer_scale)
00692                 macroblock_modes &= ~MACROBLOCK_QUANT;
00693 
00694             if ( first_in_slice )
00695             {
00696                 put_quantiser( tr );
00697                 bs_write( bs, 0, 1 );
00698                 macroblock_modes &= ~MACROBLOCK_QUANT;
00699             }
00700             putaddrinc( tr, mba_inc );
00701             mba_inc = 0;
00702             putmbdata( tr, macroblock_modes );
00703             if( macroblock_modes & MACROBLOCK_QUANT )
00704             {
00705                 put_quantiser( tr );
00706             }
00707 
00708             // put saved motion data...
00709             for (batb = 0; batb < (p_n_w - p_n_ow); batb++)
00710             {
00711                 bs_write( bs, p_n_ow[batb], 8 );
00712             }
00713             bs_write( bs, i_n_bit_out_cache, BITS_IN_BUF - i_n_bit_out );
00714             // end saved motion data...
00715 
00716             for ( i = 0; i < 6; i++ )
00717             {
00718                 bs_write( bs, *(dc + i), *(dc_len + i) );
00719                 putintrablk( bs, new_block[i], tr->intra_vlc_format );
00720             }
00721     
00722         }
00723         else
00724         {
00725             RunLevel block[6][65]; // terminated by level = 0, so we need 64+1
00726             RunLevel new_block[6][65]; // terminated by level = 0, so we need 64+1
00727             int new_coded_block_pattern = 0;
00728             int cbp = 0;
00729 
00730             // begin saving data
00731             int batb;
00732             uint8_t   p_n_ow[32], *p_n_w,
00733                     *p_o_ow = bs->p_ow, *p_o_w = bs->p_w;
00734             uint32_t  i_n_bit_out, i_n_bit_out_cache,
00735                     i_o_bit_out  = bs->i_bit_out, i_o_bit_out_cache = bs->i_bit_out_cache;
00736 
00737             bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
00738             bs->p_ow = bs->p_w = p_n_ow;
00739 
00740             if (tr->picture_structure == FRAME_PICTURE)
00741                 switch (macroblock_modes & MOTION_TYPE_MASK)
00742                 {
00743                     case MC_FRAME: MOTION_CALL (motion_fr_frame, macroblock_modes); break;
00744                     case MC_FIELD: MOTION_CALL (motion_fr_field, macroblock_modes); break;
00745                     case MC_DMV: MOTION_CALL (motion_fr_dmv, MACROBLOCK_MOTION_FORWARD); break;
00746                 }
00747             else
00748                 switch (macroblock_modes & MOTION_TYPE_MASK)
00749                 {
00750                     case MC_FIELD: MOTION_CALL (motion_fi_field, macroblock_modes); break;
00751                     case MC_16X8: MOTION_CALL (motion_fi_16x8, macroblock_modes); break;
00752                     case MC_DMV: MOTION_CALL (motion_fi_dmv, MACROBLOCK_MOTION_FORWARD); break;
00753                 }
00754 
00755             //LOG("non intra "); if (macroblock_modes & MACROBLOCK_QUANT) LOGF("got new quant: %i ", quantizer_scale);
00756 
00757             if (macroblock_modes & MACROBLOCK_PATTERN)
00758             {
00759                 int last_in_slice;
00760 
00761                 cbp = get_coded_block_pattern( bs );
00762 
00763                 for ( i = 0; i < 6; i++ )
00764                 {
00765                     if ( cbp & (1 << (5 - i)) )
00766                     {
00767                         get_non_intra_block( tr, block[i] );
00768                         if (tr->b_error) return;
00769                     }
00770                 }
00771                 last_in_slice = !UBITS( bs->i_bit_in_cache, 11 );
00772 
00773                 new_coded_block_pattern = transrate_mb( tr, block, new_block,
00774                                                         cbp, 0 );
00775 
00776                 if ( !new_coded_block_pattern &&
00777                         !(macroblock_modes
00778                             & (MACROBLOCK_MOTION_FORWARD
00779                                 | MACROBLOCK_MOTION_BACKWARD))
00780                         && (first_in_slice || last_in_slice) )
00781                 {
00782                     /* First mb in slice, just code a 0-mv mb.
00783                      * This is wrong for last in slice, but it only shows
00784                      * a few artefacts. */
00785                     macroblock_modes |= MACROBLOCK_MOTION_FORWARD;
00786                     if (tr->picture_structure == FRAME_PICTURE)
00787                     {
00788                         macroblock_modes |= MC_FRAME;
00789                         bs_write( bs, 0x3, 2 ); /* motion vectors */
00790                     }
00791                     else
00792                     {
00793                         macroblock_modes |= MC_FIELD;
00794                         bs_write( bs,
00795                              (tr->picture_structure == BOTTOM_FIELD ? 1 : 0),
00796                              1); /* motion field select */
00797                         bs_write( bs, 0x3, 2 ); /* motion vectors */
00798                     }
00799                 }
00800 
00801                 if ( !new_coded_block_pattern )
00802                 {
00803                     macroblock_modes &= ~MACROBLOCK_PATTERN;
00804                     macroblock_modes &= ~MACROBLOCK_QUANT;
00805                 }
00806                 else
00807                 {
00808                     if ( tr->last_coded_scale == tr->new_quantizer_scale )
00809                     {
00810                         macroblock_modes &= ~MACROBLOCK_QUANT;
00811                     }
00812                     else
00813                     {
00814                         macroblock_modes |= MACROBLOCK_QUANT;
00815                     }
00816                 }
00817             }
00818 
00819             assert(bs->p_w - bs->p_ow < 32);
00820 
00821             p_n_w = bs->p_w;
00822             i_n_bit_out = bs->i_bit_out;
00823             i_n_bit_out_cache = bs->i_bit_out_cache;
00824             assert(bs->p_ow == p_n_ow);
00825 
00826             bs->i_bit_out = i_o_bit_out ;
00827             bs->i_bit_out_cache = i_o_bit_out_cache;
00828             bs->p_ow = p_o_ow;
00829             bs->p_w = p_o_w;
00830             // end saving data
00831 
00832             if ( macroblock_modes &
00833                     (MACROBLOCK_MOTION_FORWARD | MACROBLOCK_MOTION_BACKWARD
00834                       | MACROBLOCK_PATTERN) )
00835             {
00836                 if ( first_in_slice )
00837                 {
00838                     put_quantiser( tr );
00839                     bs_write( bs, 0, 1 );
00840                     macroblock_modes &= ~MACROBLOCK_QUANT;
00841                 }
00842                 putaddrinc( tr, mba_inc );
00843                 mba_inc = 0;
00844                 putmbdata( tr, macroblock_modes );
00845                 if ( macroblock_modes & MACROBLOCK_QUANT )
00846                 {
00847                     put_quantiser( tr );
00848                 }
00849 
00850                 // put saved motion data...
00851                 for (batb = 0; batb < (p_n_w - p_n_ow); batb++)
00852                 {
00853                     bs_write( bs, p_n_ow[batb], 8 );
00854                 }
00855                 bs_write( bs, i_n_bit_out_cache, BITS_IN_BUF - i_n_bit_out);
00856                 // end saved motion data...
00857 
00858                 if (macroblock_modes & MACROBLOCK_PATTERN)
00859                 {
00860                     /* Write CBP */
00861                     bs_write( bs, cbptable[new_coded_block_pattern].code,
00862                               cbptable[new_coded_block_pattern].len );
00863 
00864                     for ( i = 0; i < 6; i++ )
00865                     {
00866                         if ( new_coded_block_pattern & (1 << (5 - i)) )
00867                         {
00868                             putnonintrablk( bs, new_block[i] );
00869                         }
00870                     }
00871                 }
00872             }
00873             else
00874             {
00875                 /* skipped macroblock */
00876                 mba_inc++;
00877             }
00878         }
00879 
00880         if (bs->p_c > bs->p_r || bs->p_w > bs->p_rw)
00881         {
00882             tr->b_error = 1;
00883             return;
00884         }
00885         //LOGF("\n\to: %i c: %i n: %i\n", quantizer_scale, last_coded_scale, new_quantizer_scale);
00886 
00887         NEXT_MACROBLOCK;
00888 
00889         first_in_slice = 0;
00890         mba_local = 0;
00891         for ( ; ; )
00892         {
00893             if ( bs->i_bit_in_cache >= 0x10000000 )
00894             {
00895                 mba = MBA_5 + (UBITS (bs->i_bit_in_cache, 5) - 2);
00896                 break;
00897             }
00898             else if ( bs->i_bit_in_cache >= 0x03000000 )
00899             {
00900                 mba = MBA_11 + (UBITS (bs->i_bit_in_cache, 11) - 24);
00901                 break;
00902             }
00903             else if ( UBITS( bs->i_bit_in_cache, 11 ) == 8 )
00904             {
00905                 /* macroblock_escape */
00906                 mba_inc += 33;
00907                 mba_local += 33;
00908                 bs_flush(bs, 11);
00909             }
00910             else
00911             {
00912                 /* EOS or error */
00913                 return;
00914             }
00915         }
00916         bs_flush(bs, mba->len);
00917         mba_inc += mba->mba;
00918         mba_local += mba->mba;
00919 
00920         while( mba_local-- )
00921         {
00922             NEXT_MACROBLOCK;
00923         }
00924     }
00925 }
00926 
00927 static const uint8_t mpeg2_scan_norm[64] ATTR_ALIGN(16) = {
00928     /* Zig-Zag scan pattern */
00929      0,  1,  8, 16,  9,  2,  3, 10, 17, 24, 32, 25, 18, 11,  4,  5,
00930     12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13,  6,  7, 14, 21, 28,
00931     35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
00932     58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63
00933 };
00934 
00935 static const uint8_t mpeg2_scan_alt[64] ATTR_ALIGN(16) = {
00936     /* Alternate scan pattern */
00937      0, 8,  16, 24,  1,  9,  2, 10, 17, 25, 32, 40, 48, 56, 57, 49,
00938     41, 33, 26, 18,  3, 11,  4, 12, 19, 27, 34, 42, 50, 58, 35, 43,
00939     51, 59, 20, 28,  5, 13,  6, 14, 21, 29, 36, 44, 52, 60, 37, 45,
00940     53, 61, 22, 30,  7, 15, 23, 31, 38, 46, 54, 62, 39, 47, 55, 63
00941 };
00942 
00943 static const int16_t default_intra_matrix[64] = {
00944         8, 16, 19, 22, 26, 27, 29, 34,
00945         16, 16, 22, 24, 27, 29, 34, 37,
00946         19, 22, 26, 27, 29, 34, 34, 38,
00947         22, 22, 26, 27, 29, 34, 37, 40,
00948         22, 26, 27, 29, 32, 35, 40, 48,
00949         26, 27, 29, 32, 35, 40, 48, 58,
00950         26, 27, 29, 34, 38, 46, 56, 69,
00951         27, 29, 35, 38, 46, 56, 69, 83
00952 };
00953 
00954 static int mpeg2_header_sequence( transrate_t * tr )
00955 {
00956     bs_transrate_t *bs = &tr->bs;
00957     int has_intra = 0, has_non_intra = 0;
00958     int i;
00959 
00960     i = (bs->p_c[0] << 16) | (bs->p_c[1] << 8) | bs->p_c[2];
00961     tr->horizontal_size_value = i >> 12;
00962     tr->vertical_size_value = i & 0xfff;
00963     tr->horizontal_size_value = (tr->horizontal_size_value + 15) & ~15;
00964     tr->vertical_size_value = (tr->vertical_size_value + 15) & ~15;
00965     if ( !tr->horizontal_size_value || !tr->vertical_size_value )
00966     {
00967         return -1;
00968     }
00969 
00970     if ( tr->mpeg4_matrix )
00971     {
00972         if (bs->p_c[7] & 2)
00973         {
00974             has_intra = 1;
00975             for (i = 0; i < 64; i++)
00976                 tr->intra_quantizer_matrix[mpeg2_scan_norm[i]] =
00977                 (bs->p_c[i+7] << 7) | (bs->p_c[i+8] >> 1);
00978         }
00979         else
00980         {
00981             for (i = 0; i < 64; i++)
00982                 tr->intra_quantizer_matrix[mpeg2_scan_norm[i]] =
00983                 default_intra_matrix[i];
00984         }
00985 
00986         if (bs->p_c[7+64] & 1)
00987         {
00988             has_non_intra = 1;
00989             for (i = 0; i < 64; i++)
00990                 tr->non_intra_quantizer_matrix[mpeg2_scan_norm[i]] =
00991                 bs->p_c[i+8+64];
00992         }
00993         else
00994         {
00995             for (i = 0; i < 64; i++)
00996                 tr->non_intra_quantizer_matrix[i] = 16;
00997         }
00998     }
00999 
01000     /* Write quantization matrices */
01001     memcpy( bs->p_w, bs->p_c, 8 );
01002     bs->p_c += 8;
01003 
01004     if ( tr->mpeg4_matrix )
01005     {
01006         memset( &bs->p_w[8], 0, 128 );
01007         bs->p_w[7] |= 2;
01008         bs->p_w[7] &= ~1;
01009         for (i = 0; i < 64; i++)
01010         {
01011             bs->p_w[i+7] |= mpeg4_default_intra_matrix[mpeg2_scan_norm[i]] >> 7;
01012             bs->p_w[i+8] |= mpeg4_default_intra_matrix[mpeg2_scan_norm[i]] << 1;
01013         }
01014 
01015         bs->p_w[7+64] |= 1;
01016         for (i = 0; i < 64; i++)
01017         {
01018             bs->p_w[i+8+64] |= mpeg4_default_intra_matrix[mpeg2_scan_norm[i]];
01019         }
01020         bs->p_w += 8 + 128;
01021         bs->p_c += (has_intra + has_non_intra) * 64;
01022     }
01023     else
01024     {
01025         bs->p_w += 8;
01026     }
01027 
01028     tr->scan = mpeg2_scan_norm;
01029 
01030     return 0;
01031 }
01032 
01034 
01035 static int do_next_start_code( transrate_t *tr )
01036 {
01037     bs_transrate_t *bs = &tr->bs;
01038     uint8_t ID;
01039 
01040     // get start code
01041     ID = bs->p_c[0];
01042 
01043     /* Copy one byte */
01044     *bs->p_w++ = *bs->p_c++;
01045 
01046     if (ID == 0x00) // pic header
01047     {
01048         tr->picture_coding_type = (bs->p_c[1] >> 3) & 0x7;
01049         bs->p_c[1] |= 0x7; bs->p_c[2] = 0xFF; bs->p_c[3] |= 0xF8; // vbv_delay is now 0xFFFF
01050 
01051         memcpy(bs->p_w, bs->p_c, 4);
01052         bs->p_c += 4;
01053         bs->p_w += 4;
01054     }
01055     else if (ID == 0xB3) // seq header
01056     {
01057         mpeg2_header_sequence(tr);
01058     }
01059     else if (ID == 0xB5) // extension
01060     {
01061         if ((bs->p_c[0] >> 4) == 0x8) // pic coding ext
01062         {
01063             tr->f_code[0][0] = (bs->p_c[0] & 0xF) - 1;
01064             tr->f_code[0][1] = (bs->p_c[1] >> 4) - 1;
01065             tr->f_code[1][0] = (bs->p_c[1] & 0xF) - 1;
01066             tr->f_code[1][1] = (bs->p_c[2] >> 4) - 1;
01067 
01068             /* tr->intra_dc_precision = (bs->p_c[2] >> 2) & 0x3; */
01069             tr->picture_structure = bs->p_c[2] & 0x3;
01070             tr->frame_pred_frame_dct = (bs->p_c[3] >> 6) & 0x1;
01071             tr->concealment_motion_vectors = (bs->p_c[3] >> 5) & 0x1;
01072             tr->q_scale_type = (bs->p_c[3] >> 4) & 0x1;
01073             tr->intra_vlc_format = (bs->p_c[3] >> 3) & 0x1;
01074             if ( (bs->p_c[3] >> 2) & 0x1 )
01075                 tr->scan = mpeg2_scan_alt;
01076 
01077             memcpy(bs->p_w, bs->p_c, 5);
01078             bs->p_c += 5;
01079             bs->p_w += 5;
01080         }
01081         else
01082         {
01083             *bs->p_w++ = *bs->p_c++;
01084         }
01085     }
01086     else if (ID == 0xB8) // gop header
01087     {
01088         memcpy(bs->p_w, bs->p_c, 4);
01089         bs->p_c += 4;
01090         bs->p_w += 4;
01091     }
01092     else if ((ID >= 0x01) && (ID <= 0xAF)) // slice
01093     {
01094         uint8_t *outTemp = bs->p_w, *inTemp = bs->p_c;
01095 
01096         if( tr->qrate != 1.0 )
01097         {
01098             if( !tr->horizontal_size_value || !tr->vertical_size_value )
01099             {
01100                 return -1;
01101             }
01102 
01103             // init bit buffer
01104             bs->i_bit_in_cache = 0; bs->i_bit_in = 0;
01105             bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
01106 
01107             // get 32 bits
01108             bs_refill( bs );
01109             bs_refill( bs );
01110             bs_refill( bs );
01111             bs_refill( bs );
01112 
01113             // begin bit level recoding
01114             mpeg2_slice(tr, ID);
01115             if (tr->b_error) return -1;
01116 
01117             bs_flush_read( bs );
01118             bs_flush_write( bs );
01119             // end bit level recoding
01120 
01121             /* Basic sanity checks --Meuuh */
01122             if (bs->p_c > bs->p_r || bs->p_w > bs->p_rw)
01123             {
01124                 return -1;
01125             }
01126 
01127             /*LOGF("type: %s code: %02i in : %6i out : %6i diff : %6i fact: %2.2f\n",
01128             (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
01129             ID,  bs->p_c - inTemp, bs->p_w - outTemp, (bs->p_w - outTemp) - (bs->p_c - inTemp), (float)(bs->p_c - inTemp) / (float)(bs->p_w - outTemp));*/
01130 
01131             if (bs->p_w - outTemp > bs->p_c - inTemp) // yes that might happen, rarely
01132             {
01133 
01134                 /*LOGF("*** slice bigger than before !! (type: %s code: %i in : %i out : %i diff : %i)\n",
01135                 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
01136                 ID, bs->p_c - inTemp, bs->p_w - outTemp, (bs->p_w - outTemp) - (bs->p_c - inTemp));*/
01137 
01138                 if ( !tr->mpeg4_matrix )
01139                 {
01140                     // in this case, we'll just use the original slice !
01141                     memcpy(outTemp, inTemp, bs->p_c - inTemp);
01142                     bs->p_w = outTemp + (bs->p_c - inTemp);
01143 
01144                     // adjust bs->i_byte_out
01145                     bs->i_byte_out -= (bs->p_w - outTemp) - (bs->p_c - inTemp);
01146                 }
01147                 else
01148                 {
01149                     fprintf(stderr, "bad choice for mpeg4-matrix...\n");
01150                 }
01151             }
01152         }
01153     }
01154     return 0;
01155 }
01156 
01157 int process_frame( sout_stream_t *p_stream, sout_stream_id_t *id,
01158                    block_t *in, block_t **out, int i_handicap )
01159 {
01160     transrate_t *tr = &id->tr;
01161     bs_transrate_t *bs = &tr->bs;
01162 
01163     block_t       *p_out;
01164 
01165     double        f_drift, f_fact;
01166     int           i_drift;
01167 
01168     p_out = block_New( p_stream, in->i_buffer * 3 );
01169 
01170     p_out->i_length = in->i_length;
01171     p_out->i_dts    = in->i_dts;
01172     p_out->i_pts    = in->i_pts;
01173     p_out->i_flags  = in->i_flags;
01174 
01175     bs->p_rw = bs->p_ow = bs->p_w = p_out->p_buffer;
01176     bs->p_c = bs->p_r = in->p_buffer;
01177     bs->p_r += in->i_buffer + 4;
01178     bs->p_rw += in->i_buffer * 2;
01179     *(in->p_buffer + in->i_buffer) = 0;
01180     *(in->p_buffer + in->i_buffer + 1) = 0;
01181     *(in->p_buffer + in->i_buffer + 2) = 1;
01182     *(in->p_buffer + in->i_buffer + 3) = 0;
01183 
01184     /* Calculate how late we are */
01185     bs->i_byte_in = in->i_buffer;
01186     bs->i_byte_out  = 0;
01187 
01188     i_drift = tr->i_current_output + tr->i_remaining_input
01189                 - tr->i_wanted_output;
01190     f_drift = (double)i_drift / tr->i_wanted_output;
01191     f_fact = (double)(tr->i_wanted_output - tr->i_current_output)
01192                     / tr->i_remaining_input;
01193 
01194     if ( in->i_flags & BLOCK_FLAG_TYPE_I )
01195     {
01196         /* This is the last picture of the GOP ; only transrate if we're
01197          * very late. */
01198         if ( 0 && f_drift > 0.085 )
01199         {
01200             tr->i_minimum_error = (f_drift - 0.085) * 50.0 * 50.0;
01201             tr->i_admissible_error = (f_drift - 0.085) * 50.0 * 75.0;
01202             tr->qrate = 1.0 + (f_drift - 0.085) * 50.0;
01203             msg_Warn( p_stream, "transrating I %d/%d",
01204                       tr->i_minimum_error, tr->i_admissible_error );
01205         }
01206         else
01207         {
01208             tr->i_minimum_error = 0;
01209             tr->i_admissible_error = 0;
01210             tr->qrate = 1.0;
01211         }
01212     }
01213     else if ( in->i_flags & BLOCK_FLAG_TYPE_P )
01214     {
01215         if ( f_fact < 0.8 )
01216         {
01217             tr->i_minimum_error = (0.8 - f_fact) * 3000.0 + i_handicap;
01218             tr->i_admissible_error = (0.8 - f_fact) * 3500.0 + i_handicap;
01219             tr->qrate = 1.0 + (0.8 - f_fact) * 70.0;
01220         }
01221         else
01222         {
01223             tr->i_minimum_error = 0;
01224             tr->i_admissible_error = 0;
01225             tr->qrate = 1.0;
01226         }
01227     }
01228     else
01229     {
01230         if ( f_fact < 1.2 )
01231         {
01232             tr->i_minimum_error = (1.2 - f_fact) * 1750.0 + i_handicap;
01233             tr->i_admissible_error = (1.2 - f_fact) * 2250.0 + i_handicap;
01234             tr->qrate = 1.0 + (1.2 - f_fact) * 45.0;
01235         }
01236         else
01237         {
01238             tr->i_minimum_error = 0;
01239             tr->i_admissible_error = 0;
01240             tr->qrate = 1.0;
01241         }
01242     }
01243 
01244     tr->new_quantizer_scale = 0;
01245     tr->b_error = 0;
01246 
01247     for ( ; ; )
01248     {
01249         uint8_t *p_end = &in->p_buffer[in->i_buffer];
01250 
01251         /* Search next start code */
01252         for( ;; )
01253         {
01254             if( bs->p_c < p_end - 3 && bs->p_c[0] == 0 && bs->p_c[1] == 0 && bs->p_c[2] == 1 )
01255             {
01256                 /* Next start code */
01257                 break;
01258             }
01259             else if( bs->p_c < p_end - 6 &&
01260                      bs->p_c[0] == 0 && bs->p_c[1] == 0 && bs->p_c[2] == 0 &&
01261                      bs->p_c[3] == 0 && bs->p_c[4] == 0 && bs->p_c[5] == 0 )
01262             {
01263                 /* remove stuffing (looking for 6 0x00 bytes) */
01264                 bs->p_c++;
01265             }
01266             else
01267             {
01268                 /* Copy */
01269                 *bs->p_w++ = *bs->p_c++;
01270             }
01271 
01272             if( bs->p_c >= p_end )
01273             {
01274                 break;
01275             }
01276         }
01277 
01278         if( bs->p_c >= p_end )
01279         {
01280             break;
01281         }
01282 
01283         /* Copy the start code */
01284         memcpy( bs->p_w, bs->p_c, 3 );
01285         bs->p_c += 3;
01286         bs->p_w += 3;
01287 
01288         if ( do_next_start_code( tr ) )
01289         {
01290             /* Error */
01291             msg_Err( p_stream, "error in do_next_start_code()" );
01292             block_Release( p_out );
01293             tr->i_remaining_input -= in->i_buffer;
01294             tr->i_current_output += in->i_buffer;
01295             return -1;
01296         }
01297     }
01298 
01299     bs->i_byte_out += bs->p_w - bs->p_ow;
01300     p_out->i_buffer = bs->p_w - bs->p_ow;
01301 
01302 #if 0
01303     if ( in->i_flags & BLOCK_FLAG_TYPE_P && f_fact < 0.8 )
01304     {
01305         double f_ratio = (in->i_buffer - p_out->i_buffer) / in->i_buffer;
01306         if ( f_ratio < (0.8 - f_fact) * 0.1 && i_handicap < 200 )
01307         {
01308             block_Release( p_out );
01309             return process_frame( p_stream, id, in, out, i_handicap + 50 );
01310         }
01311     }
01312 
01313     if ( in->i_flags & BLOCK_FLAG_TYPE_B && f_fact < 1.1 )
01314     {
01315         double f_ratio = (double)(in->i_buffer - p_out->i_buffer)
01316                             / in->i_buffer;
01317         if ( f_ratio < (1.1 - f_fact) * 0.1 && i_handicap < 400 )
01318         {
01319 #ifdef DEBUG_TRANSRATER
01320             msg_Dbg( p_stream, "%d: %d -> %d big (f: %f d: %f)",
01321                      tr->picture_coding_type, in->i_buffer, p_out->i_buffer,
01322                      f_fact, f_drift);
01323 #endif
01324             block_Release( p_out );
01325             return process_frame( p_stream, id, in, out, i_handicap + 100 );
01326         }
01327     }
01328 #endif
01329 
01330 #if 0
01331     {
01332         int toto;
01333         for ( toto = 0; toto < p_out->i_buffer; toto++ )
01334             if (in->p_buffer[toto] != p_out->p_buffer[toto])
01335                 msg_Dbg(p_stream, "toto %d %x %x", toto, in->p_buffer[toto], p_out->p_buffer[toto]);
01336     }
01337 #endif
01338 
01339     block_ChainAppend( out, p_out );
01340     tr->i_remaining_input -= in->i_buffer;
01341     tr->i_current_output += p_out->i_buffer;
01342 
01343 #ifdef DEBUG_TRANSRATER
01344     msg_Dbg( p_stream, "%d: %d -> %d (%d/%d)",
01345              tr->picture_coding_type, in->i_buffer, p_out->i_buffer,
01346              tr->i_minimum_error, tr->i_admissible_error );
01347 #endif
01348 
01349     return 0;
01350 }
01351 
01352 

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