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

transrate.h

00001 /*****************************************************************************
00002  * transrate.h: MPEG2 video transrating module
00003  *****************************************************************************
00004  * Copyright (C) 2003 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: transrate.h 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  * sout_stream_id_t:
00033  *****************************************************************************/
00034 
00035 typedef struct
00036 {
00037     uint8_t run;
00038     short level;
00039 } RunLevel;
00040 
00041 typedef struct
00042 {
00043     uint8_t *p_c;
00044     uint8_t *p_r;
00045     uint8_t *p_w;
00046     uint8_t *p_ow;
00047     uint8_t *p_rw;
00048 
00049     int i_bit_in;
00050     int i_bit_out;
00051     uint32_t i_bit_in_cache;
00052     uint32_t i_bit_out_cache;
00053 
00054     uint32_t i_byte_in;
00055     uint32_t i_byte_out;
00056 } bs_transrate_t;
00057 
00058 typedef struct
00059 {
00060     bs_transrate_t bs;
00061 
00062     /* MPEG2 state */
00063 
00064     // seq header
00065     unsigned int horizontal_size_value;
00066     unsigned int vertical_size_value;
00067     uint8_t intra_quantizer_matrix [64];
00068     uint8_t non_intra_quantizer_matrix [64];
00069     int mpeg4_matrix;
00070 
00071     // pic header
00072     unsigned int picture_coding_type;
00073 
00074     // pic code ext
00075     unsigned int f_code[2][2];
00076     /* unsigned int intra_dc_precision; */
00077     unsigned int picture_structure;
00078     unsigned int frame_pred_frame_dct;
00079     unsigned int concealment_motion_vectors;
00080     unsigned int q_scale_type;
00081     unsigned int intra_vlc_format;
00082     const uint8_t * scan;
00083 
00084     // slice or mb
00085     // quantizer_scale_code
00086     unsigned int quantizer_scale;
00087     unsigned int new_quantizer_scale;
00088     unsigned int last_coded_scale;
00089     int   h_offset, v_offset;
00090     vlc_bool_t b_error;
00091 
00092     // mb
00093     double qrate;
00094     int i_admissible_error, i_minimum_error;
00095 
00096     /* input buffers */
00097     ssize_t i_total_input, i_remaining_input;
00098     /* output buffers */
00099     ssize_t i_current_output, i_wanted_output;
00100 } transrate_t;
00101 
00102 
00103 struct sout_stream_id_t
00104 {
00105     void            *id;
00106     vlc_bool_t      b_transrate;
00107 
00108     block_t         *p_current_buffer;
00109     block_t         *p_next_gop;
00110     mtime_t         i_next_gop_duration;
00111     size_t          i_next_gop_size;
00112 
00113     transrate_t     tr;
00114 };
00115 
00116 
00117 #ifdef HAVE_BUILTIN_EXPECT
00118 #define likely(x) __builtin_expect ((x) != 0, 1)
00119 #define unlikely(x) __builtin_expect ((x) != 0, 0)
00120 #else
00121 #define likely(x) (x)
00122 #define unlikely(x) (x)
00123 #endif
00124 
00125 #define BITS_IN_BUF (8)
00126 
00127 #define LOG(msg) fprintf (stderr, msg)
00128 #define LOGF(format, args...) fprintf (stderr, format, args)
00129 
00130 static inline void bs_write( bs_transrate_t *s, unsigned int val, int n )
00131 {
00132     assert(n < 32);
00133     assert(!(val & (0xffffffffU << n)));
00134 
00135     while (unlikely(n >= s->i_bit_out))
00136     {
00137         s->p_w[0] = (s->i_bit_out_cache << s->i_bit_out ) | (val >> (n - s->i_bit_out));
00138         s->p_w++;
00139         n -= s->i_bit_out;
00140         s->i_bit_out_cache = 0;
00141         val &= ~(0xffffffffU << n);
00142         s->i_bit_out = BITS_IN_BUF;
00143     }
00144 
00145     if (likely(n))
00146     {
00147         s->i_bit_out_cache = (s->i_bit_out_cache << n) | val;
00148         s->i_bit_out -= n;
00149     }
00150 
00151     assert(s->i_bit_out > 0);
00152     assert(s->i_bit_out <= BITS_IN_BUF);
00153 }
00154 
00155 static inline void bs_refill( bs_transrate_t *s )
00156 {
00157     assert((s->p_r - s->p_c) >= 1);
00158     s->i_bit_in_cache |= s->p_c[0] << (24 - s->i_bit_in);
00159     s->i_bit_in += 8;
00160     s->p_c++;
00161 }
00162 
00163 static inline void bs_flush( bs_transrate_t *s, unsigned int n )
00164 {
00165     assert(s->i_bit_in >= n);
00166 
00167     s->i_bit_in_cache <<= n;
00168     s->i_bit_in -= n;
00169 
00170     assert( (!n) || ((n>0) && !(s->i_bit_in_cache & 0x1)) );
00171 
00172     while (unlikely(s->i_bit_in < 24)) bs_refill( s );
00173 }
00174 
00175 static inline unsigned int bs_read( bs_transrate_t *s, unsigned int n )
00176 {
00177     unsigned int Val = ((unsigned int)s->i_bit_in_cache) >> (32 - n);
00178     bs_flush( s, n );
00179     return Val;
00180 }
00181 
00182 static inline unsigned int bs_copy( bs_transrate_t *s, unsigned int n )
00183 {
00184     unsigned int Val = bs_read( s, n);
00185     bs_write(s, Val, n);
00186     return Val;
00187 }
00188 
00189 static inline void bs_flush_read( bs_transrate_t *s )
00190 {
00191     int i = s->i_bit_in & 0x7;
00192     if( i )
00193     {
00194         assert(((unsigned int)s->i_bit_in_cache) >> (32 - i) == 0);
00195         s->i_bit_in_cache <<= i;
00196         s->i_bit_in -= i;
00197     }
00198     s->p_c += -1 * (s->i_bit_in >> 3);
00199     s->i_bit_in = 0;
00200 }
00201 static inline void bs_flush_write( bs_transrate_t *s )
00202 {
00203     if( s->i_bit_out != 8 ) bs_write(s, 0, s->i_bit_out);
00204 }
00205 
00206 int scale_quant( transrate_t *tr, double qrate );
00207 int transrate_mb( transrate_t *tr, RunLevel blk[6][65], RunLevel new_blk[6][65], int i_cbp, int intra );
00208 void get_intra_block_B14( transrate_t *tr, RunLevel *blk );
00209 void get_intra_block_B15( transrate_t *tr, RunLevel *blk );
00210 int get_non_intra_block( transrate_t *tr, RunLevel *blk );
00211 void putnonintrablk( bs_transrate_t *bs, RunLevel *blk);
00212 void putintrablk( bs_transrate_t *bs, RunLevel *blk, int vlcformat);
00213 
00214 int process_frame( sout_stream_t *p_stream, sout_stream_id_t *id,
00215                    block_t *in, block_t **out, int i_handicap );

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