Commit 29d2ca92 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Remove transrate

"[It] can be removed altogether, since it's totally useless (quality
is too bad to be of any use)."
parent 6fb014aa
...@@ -21,6 +21,7 @@ Audio filters: ...@@ -21,6 +21,7 @@ Audio filters:
Encoders: Encoders:
* x264, add psy-rd parameter and change default settings * x264, add psy-rd parameter and change default settings
* MPEG2 transrate stream output removed
Video Output: Video Output:
* Added desktop mode to the Direct3D output module. It differs from DirectX * Added desktop mode to the Direct3D output module. It differs from DirectX
......
...@@ -723,7 +723,7 @@ AC_CHECK_LIB(m,cos,[ ...@@ -723,7 +723,7 @@ AC_CHECK_LIB(m,cos,[
VLC_ADD_LIBS([adjust wave ripple psychedelic gradient a52tofloat32 dtstofloat32 x264 goom visual panoramix rotate noise grain scene],[-lm]) VLC_ADD_LIBS([adjust wave ripple psychedelic gradient a52tofloat32 dtstofloat32 x264 goom visual panoramix rotate noise grain scene],[-lm])
]) ])
AC_CHECK_LIB(m,pow,[ AC_CHECK_LIB(m,pow,[
VLC_ADD_LIBS([avcodec avformat swscale postproc ffmpegaltivec stream_out_transrate i420_rgb faad twolame equalizer spatializer param_eq libvlccore freetype mod mpc dmo quicktime realaudio realvideo opengl],[-lm]) VLC_ADD_LIBS([avcodec avformat swscale postproc ffmpegaltivec i420_rgb faad twolame equalizer spatializer param_eq libvlccore freetype mod mpc dmo quicktime realaudio realvideo opengl],[-lm])
]) ])
AC_CHECK_LIB(m,sqrt,[ AC_CHECK_LIB(m,sqrt,[
VLC_ADD_LIBS([headphone_channel_mixer normvol speex mono colorthres extract],[-lm]) VLC_ADD_LIBS([headphone_channel_mixer normvol speex mono colorthres extract],[-lm])
...@@ -5215,7 +5215,6 @@ AC_CONFIG_FILES([ ...@@ -5215,7 +5215,6 @@ AC_CONFIG_FILES([
modules/services_discovery/Makefile modules/services_discovery/Makefile
modules/stream_filter/Makefile modules/stream_filter/Makefile
modules/stream_out/Makefile modules/stream_out/Makefile
modules/stream_out/transrate/Makefile
modules/video_chroma/Makefile modules/video_chroma/Makefile
modules/video_filter/Makefile modules/video_filter/Makefile
modules/video_filter/atmo/Makefile modules/video_filter/atmo/Makefile
......
SUBDIRS = transrate
SOURCES_stream_out_dummy = dummy.c SOURCES_stream_out_dummy = dummy.c
SOURCES_stream_out_description = description.c SOURCES_stream_out_description = description.c
SOURCES_stream_out_standard = standard.c SOURCES_stream_out_standard = standard.c
......
SOURCES_stream_out_transrate = transrate.c transrate.h frame.c block.c getvlc.h putvlc.h
libvlc_LTLIBRARIES += libstream_out_transrate_plugin.la
/*****************************************************************************
* block.c: MPEG2 video transrating module
*****************************************************************************
* Copyright (C) 2003-2004 the VideoLAN team
* Copyright (C) 2003 Antoine Missout
* Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
* Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
* $Id$
*
* Authors: Christophe Massiot <massiot@via.ecp.fr>
* Laurent Aimar <fenrir@via.ecp.fr>
* Antoine Missout
* Michel Lespinasse <walken@zoy.org>
* Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
#define NDEBUG 1
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include <math.h>
#include <vlc_common.h>
#include <vlc_sout.h>
#include <vlc_codec.h>
#include "transrate.h"
/****************************************************************************
* transrater, code from M2VRequantizer http://www.metakine.com/
****************************************************************************/
/////---- begin ext mpeg code
#include "getvlc.h"
#include "putvlc.h"
static inline int saturate( int i_value )
{
if ( i_value > 2047 )
return 2047;
if ( i_value < -2048 )
return -2048;
return i_value;
}
static int64_t get_score( const RunLevel *blk, RunLevel *new_blk, int i_qscale, int i_qscale_new )
{
int64_t score = 0;
int i1 = -1, i2 = -1;
while ( new_blk->level )
{
int new_level = new_blk->level;
int level = blk->level;
if ( i1 > 64 || i2 > 64 || !blk->run || !new_blk->run ) return score;
if ( i1 + blk->run == i2 + new_blk->run )
{
int64_t tmp = saturate(level * i_qscale)
- saturate(new_level * i_qscale_new);
i1 += blk->run;
i2 += new_blk->run;
score += tmp * tmp;
blk++;
new_blk++;
}
else
{
int64_t tmp = saturate(level * i_qscale);
i1 += blk->run;
score += tmp * tmp;
blk++;
}
}
while ( blk->level )
{
int level = blk->level;
int64_t tmp = saturate(level * i_qscale);
i1 += blk->run;
score += tmp * tmp;
blk++;
}
return score;
}
static void change_qscale( const RunLevel *blk, RunLevel *new_blk, int i_qscale, int i_qscale_new, int intra )
{
int i = 0, li = 0;
int rounding;
if ( intra )
rounding = i_qscale_new / 3;
else
rounding = i_qscale_new / 6;
while ( blk->level )
{
int level = blk->level > 0 ? blk->level : -blk->level;
int new_level = saturate(level * i_qscale) / i_qscale_new;
i += blk->run;
if ( new_level )
{
new_blk->run = i - li;
new_blk->level = blk->level > 0 ? new_level : -new_level;
new_blk++;
li = i;
}
blk++;
}
new_blk->level = 0;
}
static const uint8_t non_linear_mquant_table[32] =
{
0, 1, 2, 3, 4, 5, 6, 7,
8,10,12,14,16,18,20,22,
24,28,32,36,40,44,48,52,
56,64,72,80,88,96,104,112
};
static const uint8_t map_non_linear_mquant[113] =
{
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,
16,17,17,17,18,18,18,18,19,19,19,19,20,20,20,20,21,21,21,21,22,22,
22,22,23,23,23,23,24,24,24,24,24,24,24,25,25,25,25,25,25,25,26,26,
26,26,26,26,26,26,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,29,
29,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,31,31,31,31,31
};
int scale_quant( transrate_t *tr, double qrate )
{
int i_quant = floor( tr->quantizer_scale * qrate + 0.5 );
if ( tr->q_scale_type )
{
if ( i_quant < 1 )
i_quant = 1;
if ( i_quant > 112 )
i_quant = 112;
i_quant = non_linear_mquant_table[map_non_linear_mquant[i_quant]];
}
else
{
if ( i_quant < 2 )
i_quant = 2;
if ( i_quant > 62 )
i_quant = 62;
i_quant = (i_quant / 2) * 2; // Must be *even*
}
return i_quant;
}
static int increment_quant( transrate_t *tr, int i_quant )
{
if ( tr->q_scale_type )
{
assert( i_quant >= 1 && i_quant <= 112 );
i_quant = map_non_linear_mquant[i_quant] + 1;
if ( i_quant > 31 )
i_quant = 31;
i_quant = non_linear_mquant_table[i_quant];
}
else
{
assert(!(i_quant & 1));
i_quant += 2;
if ( i_quant > 62 )
i_quant = 62;
}
return i_quant;
}
static int decrement_quant( transrate_t *tr, int i_quant )
{
if ( tr->q_scale_type )
{
assert( i_quant >= 1 && i_quant <= 112 );
i_quant = map_non_linear_mquant[i_quant] - 1;
if ( i_quant < 1 )
i_quant = 1;
i_quant = non_linear_mquant_table[i_quant];
}
else
{
assert(!(i_quant & 1));
i_quant -= 2;
if ( i_quant < 2 )
i_quant = 2;
}
return i_quant;
}
static void quantize_block( transrate_t *tr, RunLevel *new_blk, int intra )
{
RunLevel old_blk[65];
RunLevel *blk = old_blk;
const uint8_t *old_matrix, *new_matrix;
int i = 0, li = 0;
memcpy( blk, new_blk, 65 * sizeof(RunLevel) );
if ( intra )
{
old_matrix = tr->intra_quantizer_matrix;
new_matrix = mpeg4_default_intra_matrix;
}
else
{
old_matrix = tr->non_intra_quantizer_matrix;
new_matrix = mpeg4_default_non_intra_matrix;
}
while ( blk->level )
{
int level = blk->level > 0 ? blk->level : -blk->level;
int new_level = (level * old_matrix[i] + new_matrix[i]/2)
/ new_matrix[i];
i += blk->run;
if (new_level)
{
new_blk->run = i - li;
new_blk->level = blk->level > 0 ? new_level : -new_level;
new_blk++;
li = i;
}
blk++;
}
new_blk->level = 0;
}
int transrate_mb( transrate_t *tr, RunLevel blk[6][65], RunLevel new_blk[6][65],
int i_cbp, int intra )
{
int i_qscale = tr->quantizer_scale;
int i_guessed_qscale = tr->new_quantizer_scale;
int64_t i_last_error = 0;
int i_last_qscale;
int i_last_qscale_same_error = 0;
int i_direction = 0;
int i_new_cbp;
int i_nb_blocks = 0;
int i_nb_coeffs = 0;
int i;
for ( i = 0; i < 6; i++ )
{
if ( i_cbp & (1 << (5 - i)) )
{
RunLevel *cur_blk = blk[i];
i_nb_blocks++;
while ( cur_blk->level )
{
cur_blk++;
i_nb_coeffs++;
}
}
}
/* See if we can change quantizer scale */
for ( ; ; )
{
int64_t i_error = 0;
i_new_cbp = 0;
for ( i = 0; i < 6; i++ )
{
if ( i_cbp & (1 << (5 - i)) )
{
int64_t i_block_error;
change_qscale( blk[i], new_blk[i], i_qscale, i_guessed_qscale,
intra );
i_block_error = get_score( blk[i], new_blk[i],
i_qscale, i_guessed_qscale );
if ( i > 3 ) i_block_error *= 4;
if ( i_block_error > i_error )
i_error = i_block_error;
if ( new_blk[i]->level )
i_new_cbp |= (1 << (5 - i));
}
}
if ( i_error >= (int64_t)tr->i_minimum_error
&& i_error <= (int64_t)tr->i_admissible_error )
{
break;
}
if ( i_nb_coeffs <= 15 && i_error <= (int64_t)tr->i_admissible_error )
{
/* There is no interest in changing the qscale (takes up 5 bits
* we won't regain) */
break;
}
if ( !i_direction )
{
if ( i_error > (int64_t)tr->i_admissible_error )
{
i_direction = -1;
i_last_qscale = i_guessed_qscale;
i_guessed_qscale = decrement_quant( tr, i_guessed_qscale );
}
else
{
i_direction = +1;
i_last_qscale = i_guessed_qscale;
i_guessed_qscale = increment_quant( tr, i_guessed_qscale );
i_last_error = i_error;
i_last_qscale_same_error = i_last_qscale;
}
if ( i_guessed_qscale == i_last_qscale )
break;
}
else if ( i_direction < 0 )
{
if ( i_error > (int64_t)tr->i_admissible_error )
{
i_last_qscale = i_guessed_qscale;
i_guessed_qscale = decrement_quant( tr, i_guessed_qscale );
if ( i_guessed_qscale == i_last_qscale )
break;
}
else
{
break;
}
}
else
{
if ( i_error < (int64_t)tr->i_minimum_error )
{
i_last_qscale = i_guessed_qscale;
i_guessed_qscale = increment_quant( tr, i_guessed_qscale );
if ( i_error > i_last_error )
{
i_last_error = i_error;
i_last_qscale_same_error = i_last_qscale;
}
if ( i_guessed_qscale == i_last_qscale )
{
if ( i_last_error == i_error )
{
i_guessed_qscale = i_last_qscale_same_error;
if ( i_guessed_qscale == i_qscale )
{
memcpy( new_blk, blk, sizeof(RunLevel)*65*6 );
i_new_cbp = i_cbp;
}
else
{
i_new_cbp = 0;
for ( i = 0; i < 6; i++ )
{
if ( i_cbp & (1 << (5 - i)) )
{
change_qscale( blk[i], new_blk[i],
i_qscale, i_guessed_qscale,
intra );
if ( new_blk[i]->level )
i_new_cbp |= (1 << (5 - i));
}
}
}
}
break;
}
}
else
{
if ( i_error > (int64_t)tr->i_admissible_error
|| i_last_error == i_error )
{
i_guessed_qscale = i_last_qscale_same_error;
if ( i_guessed_qscale == i_qscale )
{
memcpy( new_blk, blk, sizeof(RunLevel)*65*6 );
i_new_cbp = i_cbp;
}
else
{
i_new_cbp = 0;
for ( i = 0; i < 6; i++ )
{
if ( i_cbp & (1 << (5 - i)) )
{
change_qscale( blk[i], new_blk[i],
i_qscale, i_guessed_qscale,
intra );
if ( new_blk[i]->level )
i_new_cbp |= (1 << (5 - i));
}
}
}
}
break;
}
}
}
tr->new_quantizer_scale = i_guessed_qscale;
#if 0
/* Now see if we can drop coeffs */
for ( i = 0; i < 6; i++ )
{
if ( i_new_cbp & (1 << (5 - i)) )
{
for ( ; ; )
{
RunLevel *last_blk = new_blk[i];
uint8_t old_level;
while ( last_blk[1].level )
last_blk++;
if ( last_blk == new_blk[i] )
break;
old_level = last_blk->level;
last_blk->level = 0;
i_error = get_score( blk[i], new_blk[i],
i_qscale, i_guessed_qscale );
if ( i_error > tr->i_admissible_error )
{
last_blk->level = old_level;
break;
}
}
}
}
#endif
return i_new_cbp;
}
void get_intra_block_B14( transrate_t *tr, RunLevel *blk )
{
bs_transrate_t *bs = &tr->bs;
int i, li;
int val;
const DCTtab * tab;
li = i = 0;
for( ;; )
{
if (bs->i_bit_in_cache >= 0x28000000)
{
tab = DCT_B14AC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
i += tab->run;
if (i >= 64) break; /* end of block */
normal_code:
bs_flush( bs, tab->len );
val = tab->level;
val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
blk->level = val;
blk->run = i - li - 1;
li = i;
blk++;
bs_flush( bs, 1 );
continue;
}
else if (bs->i_bit_in_cache >= 0x04000000)
{
tab = DCT_B14_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
i += tab->run;
if (i < 64) goto normal_code;
/* escape code */
i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
bs_flush( bs, 12 );
val = SBITS (bs->i_bit_in_cache, 12);
blk->level = val;
blk->run = i - li - 1;
li = i;
blk++;
bs_flush( bs, 12 );
continue;
}
else if (bs->i_bit_in_cache >= 0x02000000)
{
tab = DCT_B14_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
i += tab->run;
if (i < 64 ) goto normal_code;
}
else if (bs->i_bit_in_cache >= 0x00800000)
{
tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
i += tab->run;
if (i < 64 ) goto normal_code;
}
else if (bs->i_bit_in_cache >= 0x00200000)
{
tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
i += tab->run;
if (i < 64 ) goto normal_code;
}
else
{
tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
bs_flush( bs, 16 );
i += tab->run;
if (i < 64 ) goto normal_code;
}
fprintf(stderr, "Err in B14\n");
tr->b_error = 1;
break; /* illegal, check needed to avoid buffer overflow */
}
bs_flush( bs, 2 ); /* dump end of block code */
blk->level = 0;
if ( tr->mpeg4_matrix )
quantize_block( tr, blk, 1 );
}
void get_intra_block_B15( transrate_t *tr, RunLevel *blk )
{
bs_transrate_t *bs = &tr->bs;
int i, li;
int val;
const DCTtab * tab;
li = i = 0;
for( ;; )
{
if (bs->i_bit_in_cache >= 0x04000000)
{
tab = DCT_B15_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
i += tab->run;
if (i < 64)
{
normal_code:
bs_flush( bs, tab->len );
val = tab->level;
val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
blk->level = val;
blk->run = i - li - 1;
li = i;
blk++;
bs_flush( bs, 1 );
continue;
}
else
{
i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
if (i >= 64) break; /* illegal, check against buffer overflow */
bs_flush( bs, 12 );
val = SBITS (bs->i_bit_in_cache, 12);
blk->level = val;
blk->run = i - li - 1;
li = i;
blk++;
bs_flush( bs, 12 );
continue;
}
}
else if (bs->i_bit_in_cache >= 0x02000000)
{
tab = DCT_B15_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
i += tab->run;
if (i < 64) goto normal_code;
}
else if (bs->i_bit_in_cache >= 0x00800000)
{
tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
i += tab->run;
if (i < 64) goto normal_code;
}
else if (bs->i_bit_in_cache >= 0x00200000)
{
tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
i += tab->run;
if (i < 64) goto normal_code;
}
else
{
tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
bs_flush( bs, 16 );
i += tab->run;
if (i < 64) goto normal_code;
}
fprintf(stderr, "Err in B15\n");
tr->b_error = 1;
break; /* illegal, check needed to avoid buffer overflow */
}
bs_flush( bs, 4 ); /* dump end of block code */
blk->level = 0;
if ( tr->mpeg4_matrix )
quantize_block( tr, blk, 1 );
}
int get_non_intra_block( transrate_t *tr, RunLevel *blk )
{
bs_transrate_t *bs = &tr->bs;
int i, li;
int val;
const DCTtab * tab;
li = i = -1;
if (bs->i_bit_in_cache >= 0x28000000)
{
tab = DCT_B14DC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
goto entry_1;
}
else goto entry_2;
for( ;; )
{
if (bs->i_bit_in_cache >= 0x28000000)
{
tab = DCT_B14AC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
entry_1:
i += tab->run;
if (i >= 64)
break; /* end of block */
normal_code:
bs_flush( bs, tab->len );
val = tab->level;
val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
blk->level = val;
blk->run = i - li - 1;
li = i;
blk++;
//if ( ((val) && (tab->level < tst)) || ((!val) && (tab->level >= tst)) )
// LOGF("level: %i val: %i tst : %i q: %i nq : %i\n", tab->level, val, tst, q, nq);
bs_flush( bs, 1 );
continue;
}
entry_2:
if (bs->i_bit_in_cache >= 0x04000000)
{
tab = DCT_B14_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
i += tab->run;
if (i < 64) goto normal_code;
/* escape code */
i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
bs_flush( bs, 12 );
val = SBITS (bs->i_bit_in_cache, 12);
blk->level = val;
blk->run = i - li - 1;
li = i;
blk++;
bs_flush( bs, 12 );
continue;
}
else if (bs->i_bit_in_cache >= 0x02000000)
{
tab = DCT_B14_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
i += tab->run;
if (i < 64) goto normal_code;
}
else if (bs->i_bit_in_cache >= 0x00800000)
{
tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
i += tab->run;
if (i < 64) goto normal_code;
}
else if (bs->i_bit_in_cache >= 0x00200000)
{
tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
i += tab->run;
if (i < 64) goto normal_code;
}
else
{
tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
bs_flush( bs, 16 );
i += tab->run;
if (i < 64) goto normal_code;
}
fprintf(stderr, "Err in non-intra\n");
tr->b_error = 1;
break; /* illegal, check needed to avoid buffer overflow */
}
bs_flush( bs, 2 ); /* dump end of block code */
blk->level = 0;
if ( tr->mpeg4_matrix )
quantize_block( tr, blk, 0 );
return i;
}
static inline void putAC( bs_transrate_t *bs, int run, int signed_level, int vlcformat)
{
int level, len;
const VLCtable *ptab = NULL;
level = (signed_level<0) ? -signed_level : signed_level; /* abs(signed_level) */
assert(!(run<0 || run>63 || level==0 || level>2047));
len = 0;
if (run<2 && level<41)
{
if (vlcformat) ptab = &dct_code_tab1a[run][level-1];
else ptab = &dct_code_tab1[run][level-1];
len = ptab->len;
}
else if (run<32 && level<6)
{
if (vlcformat) ptab = &dct_code_tab2a[run-2][level-1];
else ptab = &dct_code_tab2[run-2][level-1];
len = ptab->len;
}
if (len) /* a VLC code exists */
{
bs_write( bs, ptab->code, len);
bs_write( bs, signed_level<0, 1); /* sign */
}
else
{
bs_write( bs, 1l, 6); /* Escape */
bs_write( bs, run, 6); /* 6 bit code for run */
bs_write( bs, ((unsigned int)signed_level) & 0xFFF, 12);
}
}
static inline void putACfirst( bs_transrate_t *bs, int run, int val)
{
if (run==0 && (val==1 || val==-1)) bs_write( bs, 2|(val<0), 2 );
else putAC( bs, run, val, 0);
}
void putnonintrablk( bs_transrate_t *bs, RunLevel *blk)
{
assert(blk->level);
putACfirst( bs, blk->run, blk->level );
blk++;
while (blk->level)
{
putAC( bs, blk->run, blk->level, 0 );
blk++;
}
bs_write( bs, 2, 2 );
}
void putintrablk( bs_transrate_t *bs, RunLevel *blk, int vlcformat)
{
while (blk->level)
{
putAC( bs, blk->run, blk->level, vlcformat );
blk++;
}
if (vlcformat)
bs_write( bs, 6, 4 );
else
bs_write( bs, 2, 2 );
}
/*****************************************************************************
* frame.c: MPEG2 video transrating module
*****************************************************************************
* Copyright (C) 2003-2004 the VideoLAN team
* Copyright (C) 2003 Antoine Missout <antoine.missout@metakine.com>
* Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
* Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
* $Id$
*
* Authors: Christophe Massiot <massiot@via.ecp.fr>
* Laurent Aimar <fenrir@via.ecp.fr>
* Antoine Missout <antoine.missout@metakine.com>
* Michel Lespinasse <walken@zoy.org>
* Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
#define NDEBUG 1
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include <math.h>
#include <vlc_common.h>
#include <vlc_sout.h>
#include <vlc_codec.h>
#include "transrate.h"
/****************************************************************************
* transrater, code from M2VRequantizer http://www.metakine.com/
****************************************************************************/
// useful constants
enum
{
I_TYPE = 1,
P_TYPE = 2,
B_TYPE = 3
};
/////---- begin ext mpeg code
#include "putvlc.h"
#include "getvlc.h"
static const int non_linear_quantizer_scale [] =
{
0, 1, 2, 3, 4, 5, 6, 7,
8, 10, 12, 14, 16, 18, 20, 22,
24, 28, 32, 36, 40, 44, 48, 52,
56, 64, 72, 80, 88, 96, 104, 112
};
static inline int get_macroblock_modes( transrate_t *tr )
{
bs_transrate_t *bs = &tr->bs;
int macroblock_modes;
const MBtab * tab;
switch( tr->picture_coding_type )
{
case I_TYPE:
tab = MB_I + UBITS (bs->i_bit_in_cache, 1);
bs_flush( bs, tab->len );
macroblock_modes = tab->modes;
if ((!(tr->frame_pred_frame_dct)) && (tr->picture_structure == FRAME_PICTURE))
{
macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
bs_flush( bs, 1 );
}
return macroblock_modes;
case P_TYPE:
tab = MB_P + UBITS (bs->i_bit_in_cache, 5);
bs_flush( bs, tab->len );
macroblock_modes = tab->modes;
if (tr->picture_structure != FRAME_PICTURE)
{
if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
{
macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
bs_flush( bs, 2 );
}
return macroblock_modes;
}
else if (tr->frame_pred_frame_dct)
{
if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
macroblock_modes |= MC_FRAME;
return macroblock_modes;
}
else
{
if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
{
macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
bs_flush( bs, 2 );
}
if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
{
macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
bs_flush( bs, 1 );
}
return macroblock_modes;
}
case B_TYPE:
tab = MB_B + UBITS (bs->i_bit_in_cache, 6);
bs_flush( bs, tab->len );
macroblock_modes = tab->modes;
if( tr->picture_structure != FRAME_PICTURE)
{
if (! (macroblock_modes & MACROBLOCK_INTRA))
{
macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
bs_flush( bs, 2 );
}
return macroblock_modes;
}
else if (tr->frame_pred_frame_dct)
{
/* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
macroblock_modes |= MC_FRAME;
return macroblock_modes;
}
else
{
if (macroblock_modes & MACROBLOCK_INTRA) goto intra;
macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
bs_flush( bs, 2 );
if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
{
intra:
macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
bs_flush( bs, 1 );
}
return macroblock_modes;
}
default:
return 0;
}
}
static inline int get_quantizer_scale( transrate_t *tr )
{
bs_transrate_t *bs = &tr->bs;
int quantizer_scale_code;
quantizer_scale_code = UBITS (bs->i_bit_in_cache, 5);
bs_flush( bs, 5 );
if( tr->q_scale_type )
return non_linear_quantizer_scale[quantizer_scale_code];
else
return quantizer_scale_code << 1;
}
static inline int get_motion_delta( bs_transrate_t *bs, const int f_code )
{
int delta;
int sign;
const MVtab * tab;
if (bs->i_bit_in_cache & 0x80000000)
{
bs_copy( bs, 1 );
return 0;
}
else if (bs->i_bit_in_cache >= 0x0c000000)
{
tab = MV_4 + UBITS (bs->i_bit_in_cache, 4);
delta = (tab->delta << f_code) + 1;
bs_copy( bs, tab->len);
sign = SBITS (bs->i_bit_in_cache, 1);
bs_copy( bs, 1 );
if (f_code)
{
delta += UBITS (bs->i_bit_in_cache, f_code);
bs_copy( bs, f_code);
}
return (delta ^ sign) - sign;
}
else
{
tab = MV_10 + UBITS (bs->i_bit_in_cache, 10);
delta = (tab->delta << f_code) + 1;
bs_copy( bs, tab->len);
sign = SBITS (bs->i_bit_in_cache, 1);
bs_copy( bs, 1);
if (f_code)
{
delta += UBITS (bs->i_bit_in_cache, f_code);
bs_copy( bs, f_code);
}
return (delta ^ sign) - sign;
}
}
static inline int get_dmv( bs_transrate_t *bs )
{
const DMVtab * tab;
tab = DMV_2 + UBITS (bs->i_bit_in_cache, 2);
bs_copy( bs, tab->len);
return tab->dmv;
}
static inline int get_coded_block_pattern( bs_transrate_t *bs )
{
const CBPtab * tab;
if (bs->i_bit_in_cache >= 0x20000000)
{
tab = CBP_7 + (UBITS (bs->i_bit_in_cache, 7) - 16);
bs_flush( bs, tab->len );
return tab->cbp;
}
else
{
tab = CBP_9 + UBITS (bs->i_bit_in_cache, 9);
bs_flush( bs, tab->len );
return tab->cbp;
}
}
static inline int get_luma_dc_dct_diff( bs_transrate_t *bs, uint32_t *bits, uint8_t *len )
{
const DCtab * tab;
int size;
int dc_diff;
if (bs->i_bit_in_cache < 0xf8000000)
{
tab = DC_lum_5 + UBITS (bs->i_bit_in_cache, 5);
size = tab->size;
if (size)
{
*bits = bs_read( bs, tab->len );
*len = tab->len;
//dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
dc_diff = UBITS (bs->i_bit_in_cache, size);
if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
*bits <<= size;
*bits |= bs_read( bs, size );
*len += size;
return dc_diff;
}
else
{
*bits = bs_read( bs, 3 );
*len = 3;
return 0;
}
}
else
{
tab = DC_long + (UBITS (bs->i_bit_in_cache, 9) - 0x1e0);
size = tab->size;
*bits = bs_read( bs, tab->len );
*len = tab->len;
//dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
dc_diff = UBITS (bs->i_bit_in_cache, size);
if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
*bits <<= size;
*bits |= bs_read( bs, size );
*len += size;
return dc_diff;
}
}
static inline int get_chroma_dc_dct_diff( bs_transrate_t *bs, uint32_t *bits, uint8_t *len )
{
const DCtab * tab;
int size;
int dc_diff;
if (bs->i_bit_in_cache < 0xf8000000)
{
tab = DC_chrom_5 + UBITS (bs->i_bit_in_cache, 5);
size = tab->size;
if (size)
{
*bits = bs_read( bs, tab->len );
*len = tab->len;
//dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
dc_diff = UBITS (bs->i_bit_in_cache, size);
if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
*bits <<= size;
*bits |= bs_read( bs, size );
*len += size;
return dc_diff;
}
else
{
*bits = bs_read( bs, 2 );
*len = 2;
return 0;
}
}
else
{
tab = DC_long + (UBITS (bs->i_bit_in_cache, 10) - 0x3e0);
size = tab->size;
*bits = bs_read( bs, tab->len + 1 );
*len = tab->len + 1;
//dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
dc_diff = UBITS (bs->i_bit_in_cache, size);
if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
*bits <<= size;
*bits |= bs_read( bs, size );
*len += size;
return dc_diff;
}
}
static void motion_fr_frame( bs_transrate_t *bs, unsigned int f_code[2] )
{
get_motion_delta( bs, f_code[0] );
get_motion_delta( bs, f_code[1] );
}
static void motion_fr_field( bs_transrate_t *bs, unsigned int f_code[2] )
{
bs_copy( bs, 1);
get_motion_delta( bs, f_code[0]);
get_motion_delta( bs, f_code[1]);
bs_copy( bs, 1);
get_motion_delta( bs, f_code[0]);
get_motion_delta( bs, f_code[1]);
}
static void motion_fr_dmv( bs_transrate_t *bs, unsigned int f_code[2] )
{
get_motion_delta( bs, f_code[0]);
get_dmv( bs );
get_motion_delta( bs, f_code[1]);
get_dmv( bs );
}
static void motion_fi_field( bs_transrate_t *bs, unsigned int f_code[2] )
{
bs_copy( bs, 1);
get_motion_delta( bs, f_code[0]);
get_motion_delta( bs, f_code[1]);
}
static void motion_fi_16x8( bs_transrate_t *bs, unsigned int f_code[2] )
{
bs_copy( bs, 1);
get_motion_delta( bs, f_code[0]);
get_motion_delta( bs, f_code[1]);
bs_copy( bs, 1);
get_motion_delta( bs, f_code[0]);
get_motion_delta( bs, f_code[1]);
}
static void motion_fi_dmv( bs_transrate_t *bs, unsigned int f_code[2] )
{
get_motion_delta( bs, f_code[0]);
get_dmv( bs );
get_motion_delta( bs, f_code[1]);
get_dmv( bs );
}
#define MOTION_CALL(routine,direction) \
do { \
if ((direction) & MACROBLOCK_MOTION_FORWARD) \
routine( bs, tr->f_code[0]); \
if ((direction) & MACROBLOCK_MOTION_BACKWARD) \
routine( bs, tr->f_code[1]); \
} while (0)
#define NEXT_MACROBLOCK \
do { \
tr->h_offset += 16; \
if( tr->h_offset == tr->horizontal_size_value) \
{ \
tr->v_offset += 16; \
if (tr->v_offset > (tr->vertical_size_value - 16)) return; \
tr->h_offset = 0; \
} \
} while (0)
static void putmbdata( transrate_t *tr, int macroblock_modes )
{
bs_transrate_t *bs = &tr->bs;
bs_write( bs,
mbtypetab[tr->picture_coding_type-1][macroblock_modes&0x1F].code,
mbtypetab[tr->picture_coding_type-1][macroblock_modes&0x1F].len);
switch ( tr->picture_coding_type )
{
case I_TYPE:
if ((! (tr->frame_pred_frame_dct)) && (tr->picture_structure == FRAME_PICTURE))
bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
break;
case P_TYPE:
if (tr->picture_structure != FRAME_PICTURE)
{
if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
break;
}
else if (tr->frame_pred_frame_dct) break;
else
{
if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
break;
}
case B_TYPE:
if (tr->picture_structure != FRAME_PICTURE)
{
if (! (macroblock_modes & MACROBLOCK_INTRA))
bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
break;
}
else if (tr->frame_pred_frame_dct) break;
else
{
if (macroblock_modes & MACROBLOCK_INTRA) goto intra;
bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
{
intra:
bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
}
break;
}
}
}
static const uint8_t map_non_linear_mquant[113] =
{
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,
16,17,17,17,18,18,18,18,19,19,19,19,20,20,20,20,21,21,21,21,22,22,
22,22,23,23,23,23,24,24,24,24,24,24,24,25,25,25,25,25,25,25,26,26,
26,26,26,26,26,26,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,29,
29,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,31,31,31,31,31
};
static inline void put_quantiser( transrate_t *tr )
{
bs_transrate_t *bs = &tr->bs;
bs_write( bs, tr->q_scale_type ? map_non_linear_mquant[tr->new_quantizer_scale] : tr->new_quantizer_scale >> 1, 5 );
tr->last_coded_scale = tr->new_quantizer_scale;
}
/* generate variable length code for macroblock_address_increment (6.3.16) */
static inline void putaddrinc( transrate_t *tr, int addrinc )
{
bs_transrate_t *bs = &tr->bs;
while ( addrinc >= 33 )
{
bs_write( bs, 0x08, 11 ); /* macroblock_escape */
addrinc -= 33;
}
bs_write( bs, addrinctab[addrinc].code, addrinctab[addrinc].len );
}
static int slice_init( transrate_t *tr, int code )
{
bs_transrate_t *bs = &tr->bs;
int offset;
const MBAtab * mba;
tr->v_offset = (code - 1) * 16;
tr->quantizer_scale = get_quantizer_scale( tr );
if ( tr->new_quantizer_scale < tr->quantizer_scale )
tr->new_quantizer_scale = scale_quant( tr, tr->qrate );
/*LOGF("************************\nstart of slice %i in %s picture. ori quant: %i new quant: %i\n", code,
(picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
quantizer_scale, new_quantizer_scale);*/
/* ignore intra_slice and all the extra data */
while (bs->i_bit_in_cache & 0x80000000)
{
bs_flush( bs, 9 );
}
/* decode initial macroblock address increment */
offset = 0;
for( ;; )
{
if (bs->i_bit_in_cache >= 0x08000000)
{
mba = MBA_5 + (UBITS (bs->i_bit_in_cache, 6) - 2);
break;
}
else if (bs->i_bit_in_cache >= 0x01800000)
{
mba = MBA_11 + (UBITS (bs->i_bit_in_cache, 12) - 24);
break;
}
else if( UBITS (bs->i_bit_in_cache, 12 ) == 8 )
{
/* macroblock_escape */
offset += 33;
bs_flush(bs, 11);
}
else
{
return -1;
}
}
bs_flush(bs, mba->len + 1);
tr->h_offset = (offset + mba->mba) << 4;
while( tr->h_offset - (int)tr->horizontal_size_value >= 0)
{
tr->h_offset -= tr->horizontal_size_value;
tr->v_offset += 16;
}
if( tr->v_offset > tr->vertical_size_value - 16 )
{
return -1;
}
return (offset + mba->mba);
}
static void mpeg2_slice( transrate_t *tr, const int code )
{
bs_transrate_t *bs = &tr->bs;
int mba_inc;
int first_in_slice = 1;
if( (mba_inc = slice_init( tr, code )) < 0 )
{
return;
}
for( ;; )
{
const MBAtab * mba;
int macroblock_modes;
int mba_local;
int i;
while (unlikely(bs->i_bit_in < 24)) bs_refill( bs );
macroblock_modes = get_macroblock_modes( tr );
if (macroblock_modes & MACROBLOCK_QUANT)
tr->quantizer_scale = get_quantizer_scale( tr );
if (tr->new_quantizer_scale < tr->quantizer_scale)
tr->new_quantizer_scale = scale_quant( tr, tr->qrate );
//LOGF("blk %i : ", h_offset >> 4);
if (macroblock_modes & MACROBLOCK_INTRA)
{
RunLevel block[6][65]; // terminated by level = 0, so we need 64+1
RunLevel new_block[6][65]; // terminated by level = 0, so we need 64+1
uint32_t dc[6];
uint8_t dc_len[6];
// begin saving data
int batb;
uint8_t p_n_ow[32], *p_n_w,
*p_o_ow = bs->p_ow, *p_o_w = bs->p_w;
uint32_t i_n_bit_out, i_n_bit_out_cache,
i_o_bit_out = bs->i_bit_out, i_o_bit_out_cache = bs->i_bit_out_cache;
bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
bs->p_ow = bs->p_w = p_n_ow;
//LOG("intra "); if (macroblock_modes & MACROBLOCK_QUANT) LOGF("got new quant: %i ", quantizer_scale);
if (tr->concealment_motion_vectors)
{
if (tr->picture_structure != FRAME_PICTURE)
{
bs_copy(bs, 1); /* remove field_select */
}
/* like motion_frame, but parsing without actual motion compensation */
get_motion_delta(bs, tr->f_code[0][0]);
get_motion_delta(bs, tr->f_code[0][1]);
bs_copy(bs, 1); /* remove marker_bit */
}
assert(bs->p_w - bs->p_ow < 32);
p_n_w = bs->p_w;
i_n_bit_out = bs->i_bit_out;
i_n_bit_out_cache = bs->i_bit_out_cache;
assert(bs->p_ow == p_n_ow);
bs->i_bit_out = i_o_bit_out ;
bs->i_bit_out_cache = i_o_bit_out_cache;
bs->p_ow = p_o_ow;
bs->p_w = p_o_w;
// end saving data
if( tr->intra_vlc_format )
{
/* Luma */
for ( i = 0; i < 4; i++ )
{
get_luma_dc_dct_diff( bs, dc + i, dc_len + i );
get_intra_block_B15( tr, block[i] );
if (tr->b_error) return;
}
/* Chroma */
for ( ; i < 6; i++ )
{
get_chroma_dc_dct_diff( bs, dc + i, dc_len + i );
get_intra_block_B15( tr, block[i] );
if (tr->b_error) return;
}
}
else
{
/* Luma */
for ( i = 0; i < 4; i++ )
{
get_luma_dc_dct_diff( bs, dc + i, dc_len + i );
get_intra_block_B14( tr, block[i] );
if (tr->b_error) return;
}
/* Chroma */
for ( ; i < 6; i++ )
{
get_chroma_dc_dct_diff( bs, dc + i, dc_len + i );
get_intra_block_B14( tr, block[i] );
if (tr->b_error) return;
}
}
transrate_mb( tr, block, new_block, 0x3f, 1 );
if (tr->last_coded_scale == tr->new_quantizer_scale)
macroblock_modes &= ~MACROBLOCK_QUANT;
if ( first_in_slice )
{
put_quantiser( tr );
bs_write( bs, 0, 1 );
macroblock_modes &= ~MACROBLOCK_QUANT;
}
putaddrinc( tr, mba_inc );
mba_inc = 0;
putmbdata( tr, macroblock_modes );
if( macroblock_modes & MACROBLOCK_QUANT )
{
put_quantiser( tr );
}
// put saved motion data...
for (batb = 0; batb < (p_n_w - p_n_ow); batb++)
{
bs_write( bs, p_n_ow[batb], 8 );
}
bs_write( bs, i_n_bit_out_cache, BITS_IN_BUF - i_n_bit_out );
// end saved motion data...
for ( i = 0; i < 6; i++ )
{
bs_write( bs, *(dc + i), *(dc_len + i) );
putintrablk( bs, new_block[i], tr->intra_vlc_format );
}
}
else
{
RunLevel block[6][65]; // terminated by level = 0, so we need 64+1
RunLevel new_block[6][65]; // terminated by level = 0, so we need 64+1
int new_coded_block_pattern = 0;
int cbp = 0;
// begin saving data
int batb;
uint8_t p_n_ow[32], *p_n_w,
*p_o_ow = bs->p_ow, *p_o_w = bs->p_w;
uint32_t i_n_bit_out, i_n_bit_out_cache,
i_o_bit_out = bs->i_bit_out, i_o_bit_out_cache = bs->i_bit_out_cache;
bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
bs->p_ow = bs->p_w = p_n_ow;
if (tr->picture_structure == FRAME_PICTURE)
switch (macroblock_modes & MOTION_TYPE_MASK)
{
case MC_FRAME: MOTION_CALL (motion_fr_frame, macroblock_modes); break;
case MC_FIELD: MOTION_CALL (motion_fr_field, macroblock_modes); break;
case MC_DMV: MOTION_CALL (motion_fr_dmv, MACROBLOCK_MOTION_FORWARD); break;
}
else
switch (macroblock_modes & MOTION_TYPE_MASK)
{
case MC_FIELD: MOTION_CALL (motion_fi_field, macroblock_modes); break;
case MC_16X8: MOTION_CALL (motion_fi_16x8, macroblock_modes); break;
case MC_DMV: MOTION_CALL (motion_fi_dmv, MACROBLOCK_MOTION_FORWARD); break;
}
//LOG("non intra "); if (macroblock_modes & MACROBLOCK_QUANT) LOGF("got new quant: %i ", quantizer_scale);
if (macroblock_modes & MACROBLOCK_PATTERN)
{
int last_in_slice;
cbp = get_coded_block_pattern( bs );
for ( i = 0; i < 6; i++ )
{
if ( cbp & (1 << (5 - i)) )
{
get_non_intra_block( tr, block[i] );
if (tr->b_error) return;
}
}
last_in_slice = !UBITS( bs->i_bit_in_cache, 11 );
new_coded_block_pattern = transrate_mb( tr, block, new_block,
cbp, 0 );
if ( !new_coded_block_pattern &&
!(macroblock_modes
& (MACROBLOCK_MOTION_FORWARD
| MACROBLOCK_MOTION_BACKWARD))
&& (first_in_slice || last_in_slice) )
{
/* First mb in slice, just code a 0-mv mb.
* This is wrong for last in slice, but it only shows
* a few artefacts. */
macroblock_modes |= MACROBLOCK_MOTION_FORWARD;
if (tr->picture_structure == FRAME_PICTURE)
{
macroblock_modes |= MC_FRAME;
bs_write( bs, 0x3, 2 ); /* motion vectors */
}
else
{
macroblock_modes |= MC_FIELD;
bs_write( bs,
(tr->picture_structure == BOTTOM_FIELD ? 1 : 0),
1); /* motion field select */
bs_write( bs, 0x3, 2 ); /* motion vectors */
}
}
if ( !new_coded_block_pattern )
{
macroblock_modes &= ~MACROBLOCK_PATTERN;
macroblock_modes &= ~MACROBLOCK_QUANT;
}
else
{
if ( tr->last_coded_scale == tr->new_quantizer_scale )
{
macroblock_modes &= ~MACROBLOCK_QUANT;
}
else
{
macroblock_modes |= MACROBLOCK_QUANT;
}
}
}
assert(bs->p_w - bs->p_ow < 32);
p_n_w = bs->p_w;
i_n_bit_out = bs->i_bit_out;
i_n_bit_out_cache = bs->i_bit_out_cache;
assert(bs->p_ow == p_n_ow);
bs->i_bit_out = i_o_bit_out ;
bs->i_bit_out_cache = i_o_bit_out_cache;
bs->p_ow = p_o_ow;
bs->p_w = p_o_w;
// end saving data
if ( macroblock_modes &
(MACROBLOCK_MOTION_FORWARD | MACROBLOCK_MOTION_BACKWARD
| MACROBLOCK_PATTERN) )
{
if ( first_in_slice )
{
put_quantiser( tr );
bs_write( bs, 0, 1 );
macroblock_modes &= ~MACROBLOCK_QUANT;
}
putaddrinc( tr, mba_inc );
mba_inc = 0;
putmbdata( tr, macroblock_modes );
if ( macroblock_modes & MACROBLOCK_QUANT )
{
put_quantiser( tr );
}
// put saved motion data...
for (batb = 0; batb < (p_n_w - p_n_ow); batb++)
{
bs_write( bs, p_n_ow[batb], 8 );
}
bs_write( bs, i_n_bit_out_cache, BITS_IN_BUF - i_n_bit_out);
// end saved motion data...
if (macroblock_modes & MACROBLOCK_PATTERN)
{
/* Write CBP */
bs_write( bs, cbptable[new_coded_block_pattern].code,
cbptable[new_coded_block_pattern].len );
for ( i = 0; i < 6; i++ )
{
if ( new_coded_block_pattern & (1 << (5 - i)) )
{
putnonintrablk( bs, new_block[i] );
}
}
}
}
else
{
/* skipped macroblock */
mba_inc++;
}
}
if (bs->p_c > bs->p_r || bs->p_w > bs->p_rw)
{
tr->b_error = 1;
return;
}
//LOGF("\n\to: %i c: %i n: %i\n", quantizer_scale, last_coded_scale, new_quantizer_scale);
NEXT_MACROBLOCK;
first_in_slice = 0;
mba_local = 0;
for ( ; ; )
{
if ( bs->i_bit_in_cache >= 0x10000000 )
{
mba = MBA_5 + (UBITS (bs->i_bit_in_cache, 5) - 2);
break;
}
else if ( bs->i_bit_in_cache >= 0x03000000 )
{
mba = MBA_11 + (UBITS (bs->i_bit_in_cache, 11) - 24);
break;
}
else if ( UBITS( bs->i_bit_in_cache, 11 ) == 8 )
{
/* macroblock_escape */
mba_inc += 33;
mba_local += 33;
bs_flush(bs, 11);
}
else
{
/* EOS or error */
return;
}
}
bs_flush(bs, mba->len);
mba_inc += mba->mba;
mba_local += mba->mba;
while( mba_local-- )
{
NEXT_MACROBLOCK;
}
}
}
static const uint8_t mpeg2_scan_norm[64] ATTR_ALIGN(16) = {
/* Zig-Zag scan pattern */
0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5,
12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,
35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63
};
static const uint8_t mpeg2_scan_alt[64] ATTR_ALIGN(16) = {
/* Alternate scan pattern */
0, 8, 16, 24, 1, 9, 2, 10, 17, 25, 32, 40, 48, 56, 57, 49,
41, 33, 26, 18, 3, 11, 4, 12, 19, 27, 34, 42, 50, 58, 35, 43,
51, 59, 20, 28, 5, 13, 6, 14, 21, 29, 36, 44, 52, 60, 37, 45,
53, 61, 22, 30, 7, 15, 23, 31, 38, 46, 54, 62, 39, 47, 55, 63
};
static const int16_t default_intra_matrix[64] = {
8, 16, 19, 22, 26, 27, 29, 34,
16, 16, 22, 24, 27, 29, 34, 37,
19, 22, 26, 27, 29, 34, 34, 38,
22, 22, 26, 27, 29, 34, 37, 40,
22, 26, 27, 29, 32, 35, 40, 48,
26, 27, 29, 32, 35, 40, 48, 58,
26, 27, 29, 34, 38, 46, 56, 69,
27, 29, 35, 38, 46, 56, 69, 83
};
static int mpeg2_header_sequence( transrate_t * tr )
{
bs_transrate_t *bs = &tr->bs;
int has_intra = 0, has_non_intra = 0;
int i;
i = (bs->p_c[0] << 16) | (bs->p_c[1] << 8) | bs->p_c[2];
tr->horizontal_size_value = i >> 12;
tr->vertical_size_value = i & 0xfff;
tr->horizontal_size_value = (tr->horizontal_size_value + 15) & ~15;
tr->vertical_size_value = (tr->vertical_size_value + 15) & ~15;
if ( !tr->horizontal_size_value || !tr->vertical_size_value )
{
return -1;
}
if ( tr->mpeg4_matrix )
{
if (bs->p_c[7] & 2)
{
has_intra = 1;
for (i = 0; i < 64; i++)
tr->intra_quantizer_matrix[mpeg2_scan_norm[i]] =
(bs->p_c[i+7] << 7) | (bs->p_c[i+8] >> 1);
}
else
{
for (i = 0; i < 64; i++)
tr->intra_quantizer_matrix[mpeg2_scan_norm[i]] =
default_intra_matrix[i];
}
if (bs->p_c[7+64] & 1)
{
has_non_intra = 1;
for (i = 0; i < 64; i++)
tr->non_intra_quantizer_matrix[mpeg2_scan_norm[i]] =
bs->p_c[i+8+64];
}
else
{
for (i = 0; i < 64; i++)
tr->non_intra_quantizer_matrix[i] = 16;
}
}
/* Write quantization matrices */
memcpy( bs->p_w, bs->p_c, 8 );
bs->p_c += 8;
if ( tr->mpeg4_matrix )
{
memset( &bs->p_w[8], 0, 128 );
bs->p_w[7] |= 2;
bs->p_w[7] &= ~1;
for (i = 0; i < 64; i++)
{
bs->p_w[i+7] |= mpeg4_default_intra_matrix[mpeg2_scan_norm[i]] >> 7;
bs->p_w[i+8] |= mpeg4_default_intra_matrix[mpeg2_scan_norm[i]] << 1;
}
bs->p_w[7+64] |= 1;
for (i = 0; i < 64; i++)
{
bs->p_w[i+8+64] |= mpeg4_default_intra_matrix[mpeg2_scan_norm[i]];
}
bs->p_w += 8 + 128;
bs->p_c += (has_intra + has_non_intra) * 64;
}
else
{
bs->p_w += 8;
}
tr->scan = mpeg2_scan_norm;
return 0;
}
/////---- end ext mpeg code
static int do_next_start_code( transrate_t *tr )
{
bs_transrate_t *bs = &tr->bs;
uint8_t ID;
// get start code
ID = bs->p_c[0];
/* Copy one byte */
*bs->p_w++ = *bs->p_c++;
if (ID == 0x00) // pic header
{
tr->picture_coding_type = (bs->p_c[1] >> 3) & 0x7;
bs->p_c[1] |= 0x7; bs->p_c[2] = 0xFF; bs->p_c[3] |= 0xF8; // vbv_delay is now 0xFFFF
memcpy(bs->p_w, bs->p_c, 4);
bs->p_c += 4;
bs->p_w += 4;
}
else if (ID == 0xB3) // seq header
{
mpeg2_header_sequence(tr);
}
else if (ID == 0xB5) // extension
{
if ((bs->p_c[0] >> 4) == 0x8) // pic coding ext
{
tr->f_code[0][0] = (bs->p_c[0] & 0xF) - 1;
tr->f_code[0][1] = (bs->p_c[1] >> 4) - 1;
tr->f_code[1][0] = (bs->p_c[1] & 0xF) - 1;
tr->f_code[1][1] = (bs->p_c[2] >> 4) - 1;
/* tr->intra_dc_precision = (bs->p_c[2] >> 2) & 0x3; */
tr->picture_structure = bs->p_c[2] & 0x3;
tr->frame_pred_frame_dct = (bs->p_c[3] >> 6) & 0x1;
tr->concealment_motion_vectors = (bs->p_c[3] >> 5) & 0x1;
tr->q_scale_type = (bs->p_c[3] >> 4) & 0x1;
tr->intra_vlc_format = (bs->p_c[3] >> 3) & 0x1;
if ( (bs->p_c[3] >> 2) & 0x1 )
tr->scan = mpeg2_scan_alt;
memcpy(bs->p_w, bs->p_c, 5);
bs->p_c += 5;
bs->p_w += 5;
}
else
{
*bs->p_w++ = *bs->p_c++;
}
}
else if (ID == 0xB8) // gop header
{
memcpy(bs->p_w, bs->p_c, 4);
bs->p_c += 4;
bs->p_w += 4;
}
else if ((ID >= 0x01) && (ID <= 0xAF)) // slice
{
uint8_t *outTemp = bs->p_w, *inTemp = bs->p_c;
if( tr->qrate != 1.0 )
{
if( !tr->horizontal_size_value || !tr->vertical_size_value )
{
return -1;
}
// init bit buffer
bs->i_bit_in_cache = 0; bs->i_bit_in = 0;
bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
// get 32 bits
bs_refill( bs );
bs_refill( bs );
bs_refill( bs );
bs_refill( bs );
// begin bit level recoding
mpeg2_slice(tr, ID);
if (tr->b_error) return -1;
bs_flush_read( bs );
bs_flush_write( bs );
// end bit level recoding
/* Basic sanity checks --Meuuh */
if (bs->p_c > bs->p_r || bs->p_w > bs->p_rw)
{
return -1;
}
/*LOGF("type: %s code: %02i in : %6i out : %6i diff : %6i fact: %2.2f\n",
(picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
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));*/
if (bs->p_w - outTemp > bs->p_c - inTemp) // yes that might happen, rarely
{
/*LOGF("*** slice bigger than before !! (type: %s code: %i in : %i out : %i diff : %i)\n",
(picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
ID, bs->p_c - inTemp, bs->p_w - outTemp, (bs->p_w - outTemp) - (bs->p_c - inTemp));*/
if ( !tr->mpeg4_matrix )
{
// in this case, we'll just use the original slice !
memcpy(outTemp, inTemp, bs->p_c - inTemp);
bs->p_w = outTemp + (bs->p_c - inTemp);
// adjust bs->i_byte_out
bs->i_byte_out -= (bs->p_w - outTemp) - (bs->p_c - inTemp);
}
else
{
fprintf(stderr, "bad choice for mpeg4-matrix...\n");
}
}
}
}
return 0;
}
int process_frame( sout_stream_t *p_stream, sout_stream_id_t *id,
block_t *in, block_t **out, int i_handicap )
{
transrate_t *tr = &id->tr;
bs_transrate_t *bs = &tr->bs;
block_t *p_out;
double f_drift, f_fact;
int i_drift;
p_out = block_New( p_stream, in->i_buffer * 3 );
p_out->i_length = in->i_length;
p_out->i_dts = in->i_dts;
p_out->i_pts = in->i_pts;
p_out->i_flags = in->i_flags;
bs->p_rw = bs->p_ow = bs->p_w = p_out->p_buffer;
bs->p_c = bs->p_r = in->p_buffer;
bs->p_r += in->i_buffer + 4;
bs->p_rw += in->i_buffer * 2;
*(in->p_buffer + in->i_buffer) = 0;
*(in->p_buffer + in->i_buffer + 1) = 0;
*(in->p_buffer + in->i_buffer + 2) = 1;
*(in->p_buffer + in->i_buffer + 3) = 0;
/* Calculate how late we are */
bs->i_byte_in = in->i_buffer;
bs->i_byte_out = 0;
i_drift = tr->i_current_output + tr->i_remaining_input
- tr->i_wanted_output;
f_drift = (double)i_drift / tr->i_wanted_output;
f_fact = (double)(tr->i_wanted_output - tr->i_current_output)
/ tr->i_remaining_input;
if ( in->i_flags & BLOCK_FLAG_TYPE_I )
{
/* This is the last picture of the GOP ; only transrate if we're
* very late. */
if ( 0 && f_drift > 0.085 )
{
tr->i_minimum_error = (f_drift - 0.085) * 50.0 * 50.0;
tr->i_admissible_error = (f_drift - 0.085) * 50.0 * 75.0;
tr->qrate = 1.0 + (f_drift - 0.085) * 50.0;
msg_Warn( p_stream, "transrating I %d/%d",
tr->i_minimum_error, tr->i_admissible_error );
}
else
{
tr->i_minimum_error = 0;
tr->i_admissible_error = 0;
tr->qrate = 1.0;
}
}
else if ( in->i_flags & BLOCK_FLAG_TYPE_P )
{
if ( f_fact < 0.8 )
{
tr->i_minimum_error = (0.8 - f_fact) * 3000.0 + i_handicap;
tr->i_admissible_error = (0.8 - f_fact) * 3500.0 + i_handicap;
tr->qrate = 1.0 + (0.8 - f_fact) * 70.0;
}
else
{
tr->i_minimum_error = 0;
tr->i_admissible_error = 0;
tr->qrate = 1.0;
}
}
else
{
if ( f_fact < 1.2 )
{
tr->i_minimum_error = (1.2 - f_fact) * 1750.0 + i_handicap;
tr->i_admissible_error = (1.2 - f_fact) * 2250.0 + i_handicap;
tr->qrate = 1.0 + (1.2 - f_fact) * 45.0;
}
else
{
tr->i_minimum_error = 0;
tr->i_admissible_error = 0;
tr->qrate = 1.0;
}
}
tr->new_quantizer_scale = 0;
tr->b_error = 0;
for ( ; ; )
{
uint8_t *p_end = &in->p_buffer[in->i_buffer];
/* Search next start code */
for( ;; )
{
if( bs->p_c < p_end - 3 && bs->p_c[0] == 0 && bs->p_c[1] == 0 && bs->p_c[2] == 1 )
{
/* Next start code */
break;
}
else if( bs->p_c < p_end - 6 &&
bs->p_c[0] == 0 && bs->p_c[1] == 0 && bs->p_c[2] == 0 &&
bs->p_c[3] == 0 && bs->p_c[4] == 0 && bs->p_c[5] == 0 )
{
/* remove stuffing (looking for 6 0x00 bytes) */
bs->p_c++;
}
else
{
/* Copy */
*bs->p_w++ = *bs->p_c++;
}
if( bs->p_c >= p_end )
{
break;
}
}
if( bs->p_c >= p_end )
{
break;
}
/* Copy the start code */
memcpy( bs->p_w, bs->p_c, 3 );
bs->p_c += 3;
bs->p_w += 3;
if ( do_next_start_code( tr ) )
{
/* Error */
msg_Err( p_stream, "error in do_next_start_code()" );
block_Release( p_out );
tr->i_remaining_input -= in->i_buffer;
tr->i_current_output += in->i_buffer;
return -1;
}
}
bs->i_byte_out += bs->p_w - bs->p_ow;
p_out->i_buffer = bs->p_w - bs->p_ow;
#if 0
if ( in->i_flags & BLOCK_FLAG_TYPE_P && f_fact < 0.8 )
{
double f_ratio = (in->i_buffer - p_out->i_buffer) / in->i_buffer;
if ( f_ratio < (0.8 - f_fact) * 0.1 && i_handicap < 200 )
{
block_Release( p_out );
return process_frame( p_stream, id, in, out, i_handicap + 50 );
}
}
if ( in->i_flags & BLOCK_FLAG_TYPE_B && f_fact < 1.1 )
{
double f_ratio = (double)(in->i_buffer - p_out->i_buffer)
/ in->i_buffer;
if ( f_ratio < (1.1 - f_fact) * 0.1 && i_handicap < 400 )
{
#ifdef DEBUG_TRANSRATER
msg_Dbg( p_stream, "%d: %d -> %d big (f: %f d: %f)",
tr->picture_coding_type, in->i_buffer, p_out->i_buffer,
f_fact, f_drift);
#endif
block_Release( p_out );
return process_frame( p_stream, id, in, out, i_handicap + 100 );
}
}
#endif
#if 0
{
int toto;
for ( toto = 0; toto < p_out->i_buffer; toto++ )
if (in->p_buffer[toto] != p_out->p_buffer[toto])
msg_Dbg(p_stream, "toto %d %x %x", toto, in->p_buffer[toto], p_out->p_buffer[toto]);
}
#endif
block_ChainAppend( out, p_out );
tr->i_remaining_input -= in->i_buffer;
tr->i_current_output += p_out->i_buffer;
#ifdef DEBUG_TRANSRATER
msg_Dbg( p_stream, "%d: %d -> %d (%d/%d)",
tr->picture_coding_type, in->i_buffer, p_out->i_buffer,
tr->i_minimum_error, tr->i_admissible_error );
#endif
return 0;
}
/* macroblock modes */
#define MACROBLOCK_INTRA 1
#define MACROBLOCK_PATTERN 2
#define MACROBLOCK_MOTION_BACKWARD 4
#define MACROBLOCK_MOTION_FORWARD 8
#define MACROBLOCK_QUANT 16
#define DCT_TYPE_INTERLACED 32
/* motion_type */
#define MOTION_TYPE_MASK (3*64)
#define MOTION_TYPE_BASE 64
#define MC_FIELD (1*64)
#define MC_FRAME (2*64)
#define MC_16X8 (2*64)
#define MC_DMV (3*64)
/* picture structure */
#define TOP_FIELD 1
#define BOTTOM_FIELD 2
#define FRAME_PICTURE 3
/* take num bits from the high part of bit_buf and zero extend them */
#define UBITS(bit_buf,num) (((uint32_t)(bs->i_bit_in_cache)) >> (32 - (num)))
/* take num bits from the high part of bit_buf and sign extend them */
#define SBITS(bit_buf,num) (((int32_t)(bs->i_bit_in_cache)) >> (32 - (num)))
typedef struct {
uint8_t modes;
uint8_t len;
} MBtab;
typedef struct {
uint8_t delta;
uint8_t len;
} MVtab;
typedef struct {
int8_t dmv;
uint8_t len;
} DMVtab;
typedef struct {
uint8_t cbp;
uint8_t len;
} CBPtab;
typedef struct {
uint8_t size;
uint8_t len;
} DCtab;
typedef struct {
uint8_t run;
uint8_t level;
uint8_t len;
} DCTtab;
typedef struct {
uint8_t mba;
uint8_t len;
} MBAtab;
#define INTRA MACROBLOCK_INTRA
#define QUANT MACROBLOCK_QUANT
static const MBtab MB_I [] = {
{INTRA|QUANT, 2}, {INTRA, 1}
};
#define MC MACROBLOCK_MOTION_FORWARD
#define CODED MACROBLOCK_PATTERN
static const MBtab MB_P [] = {
{INTRA|QUANT, 6}, {CODED|QUANT, 5}, {MC|CODED|QUANT, 5}, {INTRA, 5},
{MC, 3}, {MC, 3}, {MC, 3}, {MC, 3},
{CODED, 2}, {CODED, 2}, {CODED, 2}, {CODED, 2},
{CODED, 2}, {CODED, 2}, {CODED, 2}, {CODED, 2},
{MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1},
{MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1},
{MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1},
{MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}
};
#define FWD MACROBLOCK_MOTION_FORWARD
#define BWD MACROBLOCK_MOTION_BACKWARD
#define INTER MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD
static const MBtab MB_B [] = {
{0, 0}, {INTRA|QUANT, 6},
{BWD|CODED|QUANT, 6}, {FWD|CODED|QUANT, 6},
{INTER|CODED|QUANT, 5}, {INTER|CODED|QUANT, 5},
{INTRA, 5}, {INTRA, 5},
{FWD, 4}, {FWD, 4}, {FWD, 4}, {FWD, 4},
{FWD|CODED, 4}, {FWD|CODED, 4}, {FWD|CODED, 4}, {FWD|CODED, 4},
{BWD, 3}, {BWD, 3}, {BWD, 3}, {BWD, 3},
{BWD, 3}, {BWD, 3}, {BWD, 3}, {BWD, 3},
{BWD|CODED, 3}, {BWD|CODED, 3}, {BWD|CODED, 3}, {BWD|CODED, 3},
{BWD|CODED, 3}, {BWD|CODED, 3}, {BWD|CODED, 3}, {BWD|CODED, 3},
{INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2},
{INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2},
{INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2},
{INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2},
{INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2},
{INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2},
{INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2},
{INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}
};
#undef INTRA
#undef QUANT
#undef MC
#undef CODED
#undef FWD
#undef BWD
#undef INTER
static const MVtab MV_4 [] = {
{ 3, 6}, { 2, 4}, { 1, 3}, { 1, 3}, { 0, 2}, { 0, 2}, { 0, 2}, { 0, 2}
};
static const MVtab MV_10 [] = {
{ 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10},
{ 0,10}, { 0,10}, { 0,10}, { 0,10}, {15,10}, {14,10}, {13,10}, {12,10},
{11,10}, {10,10}, { 9, 9}, { 9, 9}, { 8, 9}, { 8, 9}, { 7, 9}, { 7, 9},
{ 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7},
{ 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7},
{ 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}
};
static const DMVtab DMV_2 [] = {
{ 0, 1}, { 0, 1}, { 1, 2}, {-1, 2}
};
static const CBPtab CBP_7 [] = {
{0x22, 7}, {0x12, 7}, {0x0a, 7}, {0x06, 7},
{0x21, 7}, {0x11, 7}, {0x09, 7}, {0x05, 7},
{0x3f, 6}, {0x3f, 6}, {0x03, 6}, {0x03, 6},
{0x24, 6}, {0x24, 6}, {0x18, 6}, {0x18, 6},
{0x3e, 5}, {0x3e, 5}, {0x3e, 5}, {0x3e, 5},
{0x02, 5}, {0x02, 5}, {0x02, 5}, {0x02, 5},
{0x3d, 5}, {0x3d, 5}, {0x3d, 5}, {0x3d, 5},
{0x01, 5}, {0x01, 5}, {0x01, 5}, {0x01, 5},
{0x38, 5}, {0x38, 5}, {0x38, 5}, {0x38, 5},
{0x34, 5}, {0x34, 5}, {0x34, 5}, {0x34, 5},
{0x2c, 5}, {0x2c, 5}, {0x2c, 5}, {0x2c, 5},
{0x1c, 5}, {0x1c, 5}, {0x1c, 5}, {0x1c, 5},
{0x28, 5}, {0x28, 5}, {0x28, 5}, {0x28, 5},
{0x14, 5}, {0x14, 5}, {0x14, 5}, {0x14, 5},
{0x30, 5}, {0x30, 5}, {0x30, 5}, {0x30, 5},
{0x0c, 5}, {0x0c, 5}, {0x0c, 5}, {0x0c, 5},
{0x20, 4}, {0x20, 4}, {0x20, 4}, {0x20, 4},
{0x20, 4}, {0x20, 4}, {0x20, 4}, {0x20, 4},
{0x10, 4}, {0x10, 4}, {0x10, 4}, {0x10, 4},
{0x10, 4}, {0x10, 4}, {0x10, 4}, {0x10, 4},
{0x08, 4}, {0x08, 4}, {0x08, 4}, {0x08, 4},
{0x08, 4}, {0x08, 4}, {0x08, 4}, {0x08, 4},
{0x04, 4}, {0x04, 4}, {0x04, 4}, {0x04, 4},
{0x04, 4}, {0x04, 4}, {0x04, 4}, {0x04, 4},
{0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3},
{0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3},
{0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3},
{0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3}
};
static const CBPtab CBP_9 [] = {
{0, 0}, {0x00, 9}, {0x27, 9}, {0x1b, 9},
{0x3b, 9}, {0x37, 9}, {0x2f, 9}, {0x1f, 9},
{0x3a, 8}, {0x3a, 8}, {0x36, 8}, {0x36, 8},
{0x2e, 8}, {0x2e, 8}, {0x1e, 8}, {0x1e, 8},
{0x39, 8}, {0x39, 8}, {0x35, 8}, {0x35, 8},
{0x2d, 8}, {0x2d, 8}, {0x1d, 8}, {0x1d, 8},
{0x26, 8}, {0x26, 8}, {0x1a, 8}, {0x1a, 8},
{0x25, 8}, {0x25, 8}, {0x19, 8}, {0x19, 8},
{0x2b, 8}, {0x2b, 8}, {0x17, 8}, {0x17, 8},
{0x33, 8}, {0x33, 8}, {0x0f, 8}, {0x0f, 8},
{0x2a, 8}, {0x2a, 8}, {0x16, 8}, {0x16, 8},
{0x32, 8}, {0x32, 8}, {0x0e, 8}, {0x0e, 8},
{0x29, 8}, {0x29, 8}, {0x15, 8}, {0x15, 8},
{0x31, 8}, {0x31, 8}, {0x0d, 8}, {0x0d, 8},
{0x23, 8}, {0x23, 8}, {0x13, 8}, {0x13, 8},
{0x0b, 8}, {0x0b, 8}, {0x07, 8}, {0x07, 8}
};
static const DCtab DC_lum_5 [] = {
{1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
{2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
{0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3},
{4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5}
};
static const DCtab DC_chrom_5 [] = {
{0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2},
{1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
{2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
{3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5}
};
static const DCtab DC_long [] = {
{6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, { 6, 5}, { 6, 5},
{6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, { 6, 5}, { 6, 5},
{7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, { 7, 6}, { 7, 6},
{8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10, 9}, {11, 9}
};
static const DCTtab DCT_16 [] = {
{129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
{129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
{129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
{129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
{ 2,18, 0}, { 2,17, 0}, { 2,16, 0}, { 2,15, 0},
{ 7, 3, 0}, { 17, 2, 0}, { 16, 2, 0}, { 15, 2, 0},
{ 14, 2, 0}, { 13, 2, 0}, { 12, 2, 0}, { 32, 1, 0},
{ 31, 1, 0}, { 30, 1, 0}, { 29, 1, 0}, { 28, 1, 0}
};
static const DCTtab DCT_15 [] = {
{ 1,40,15}, { 1,39,15}, { 1,38,15}, { 1,37,15},
{ 1,36,15}, { 1,35,15}, { 1,34,15}, { 1,33,15},
{ 1,32,15}, { 2,14,15}, { 2,13,15}, { 2,12,15},
{ 2,11,15}, { 2,10,15}, { 2, 9,15}, { 2, 8,15},
{ 1,31,14}, { 1,31,14}, { 1,30,14}, { 1,30,14},
{ 1,29,14}, { 1,29,14}, { 1,28,14}, { 1,28,14},
{ 1,27,14}, { 1,27,14}, { 1,26,14}, { 1,26,14},
{ 1,25,14}, { 1,25,14}, { 1,24,14}, { 1,24,14},
{ 1,23,14}, { 1,23,14}, { 1,22,14}, { 1,22,14},
{ 1,21,14}, { 1,21,14}, { 1,20,14}, { 1,20,14},
{ 1,19,14}, { 1,19,14}, { 1,18,14}, { 1,18,14},
{ 1,17,14}, { 1,17,14}, { 1,16,14}, { 1,16,14}
};
static const DCTtab DCT_13 [] = {
{ 11, 2,13}, { 10, 2,13}, { 6, 3,13}, { 4, 4,13},
{ 3, 5,13}, { 2, 7,13}, { 2, 6,13}, { 1,15,13},
{ 1,14,13}, { 1,13,13}, { 1,12,13}, { 27, 1,13},
{ 26, 1,13}, { 25, 1,13}, { 24, 1,13}, { 23, 1,13},
{ 1,11,12}, { 1,11,12}, { 9, 2,12}, { 9, 2,12},
{ 5, 3,12}, { 5, 3,12}, { 1,10,12}, { 1,10,12},
{ 3, 4,12}, { 3, 4,12}, { 8, 2,12}, { 8, 2,12},
{ 22, 1,12}, { 22, 1,12}, { 21, 1,12}, { 21, 1,12},
{ 1, 9,12}, { 1, 9,12}, { 20, 1,12}, { 20, 1,12},
{ 19, 1,12}, { 19, 1,12}, { 2, 5,12}, { 2, 5,12},
{ 4, 3,12}, { 4, 3,12}, { 1, 8,12}, { 1, 8,12},
{ 7, 2,12}, { 7, 2,12}, { 18, 1,12}, { 18, 1,12}
};
static const DCTtab DCT_B14_10 [] = {
{ 17, 1,10}, { 6, 2,10}, { 1, 7,10}, { 3, 3,10},
{ 2, 4,10}, { 16, 1,10}, { 15, 1,10}, { 5, 2,10}
};
static const DCTtab DCT_B14_8 [] = {
{ 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6},
{ 3, 2, 7}, { 3, 2, 7}, { 10, 1, 7}, { 10, 1, 7},
{ 1, 4, 7}, { 1, 4, 7}, { 9, 1, 7}, { 9, 1, 7},
{ 8, 1, 6}, { 8, 1, 6}, { 8, 1, 6}, { 8, 1, 6},
{ 7, 1, 6}, { 7, 1, 6}, { 7, 1, 6}, { 7, 1, 6},
{ 2, 2, 6}, { 2, 2, 6}, { 2, 2, 6}, { 2, 2, 6},
{ 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6},
{ 14, 1, 8}, { 1, 6, 8}, { 13, 1, 8}, { 12, 1, 8},
{ 4, 2, 8}, { 2, 3, 8}, { 1, 5, 8}, { 11, 1, 8}
};
static const DCTtab DCT_B14AC_5 [] = {
{ 1, 3, 5}, { 5, 1, 5}, { 4, 1, 5},
{ 1, 2, 4}, { 1, 2, 4}, { 3, 1, 4}, { 3, 1, 4},
{ 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
{129, 0, 2}, {129, 0, 2}, {129, 0, 2}, {129, 0, 2},
{129, 0, 2}, {129, 0, 2}, {129, 0, 2}, {129, 0, 2},
{ 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
{ 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}
};
static const DCTtab DCT_B14DC_5 [] = {
{ 1, 3, 5}, { 5, 1, 5}, { 4, 1, 5},
{ 1, 2, 4}, { 1, 2, 4}, { 3, 1, 4}, { 3, 1, 4},
{ 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
{ 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1},
{ 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1},
{ 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1},
{ 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}
};
static const DCTtab DCT_B15_10 [] = {
{ 6, 2, 9}, { 6, 2, 9}, { 15, 1, 9}, { 15, 1, 9},
{ 3, 4,10}, { 17, 1,10}, { 16, 1, 9}, { 16, 1, 9}
};
static const DCTtab DCT_B15_8 [] = {
{ 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6},
{ 8, 1, 7}, { 8, 1, 7}, { 9, 1, 7}, { 9, 1, 7},
{ 7, 1, 7}, { 7, 1, 7}, { 3, 2, 7}, { 3, 2, 7},
{ 1, 7, 6}, { 1, 7, 6}, { 1, 7, 6}, { 1, 7, 6},
{ 1, 6, 6}, { 1, 6, 6}, { 1, 6, 6}, { 1, 6, 6},
{ 5, 1, 6}, { 5, 1, 6}, { 5, 1, 6}, { 5, 1, 6},
{ 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6},
{ 2, 5, 8}, { 12, 1, 8}, { 1,11, 8}, { 1,10, 8},
{ 14, 1, 8}, { 13, 1, 8}, { 4, 2, 8}, { 2, 4, 8},
{ 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5},
{ 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5},
{ 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5},
{ 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5},
{ 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5},
{ 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5},
{ 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
{ 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
{ 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
{ 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
{ 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
{ 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
{ 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
{ 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
{129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
{129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
{129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
{129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
{ 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4},
{ 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4},
{ 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4},
{ 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4},
{ 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
{ 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
{ 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
{ 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
{ 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
{ 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
{ 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
{ 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
{ 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
{ 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
{ 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
{ 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
{ 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
{ 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
{ 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
{ 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
{ 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
{ 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
{ 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
{ 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
{ 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
{ 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
{ 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
{ 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
{ 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5},
{ 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5},
{ 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5},
{ 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5},
{ 10, 1, 7}, { 10, 1, 7}, { 2, 3, 7}, { 2, 3, 7},
{ 11, 1, 7}, { 11, 1, 7}, { 1, 8, 7}, { 1, 8, 7},
{ 1, 9, 7}, { 1, 9, 7}, { 1,12, 8}, { 1,13, 8},
{ 3, 3, 8}, { 5, 2, 8}, { 1,14, 8}, { 1,15, 8}
};
static const MBAtab MBA_5 [] = {
{6, 5}, {5, 5}, {4, 4}, {4, 4}, {3, 4}, {3, 4},
{2, 3}, {2, 3}, {2, 3}, {2, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3},
{0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1},
{0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}
};
static const MBAtab MBA_11 [] = {
{32, 11}, {31, 11}, {30, 11}, {29, 11},
{28, 11}, {27, 11}, {26, 11}, {25, 11},
{24, 11}, {23, 11}, {22, 11}, {21, 11},
{20, 10}, {20, 10}, {19, 10}, {19, 10},
{18, 10}, {18, 10}, {17, 10}, {17, 10},
{16, 10}, {16, 10}, {15, 10}, {15, 10},
{14, 8}, {14, 8}, {14, 8}, {14, 8},
{14, 8}, {14, 8}, {14, 8}, {14, 8},
{13, 8}, {13, 8}, {13, 8}, {13, 8},
{13, 8}, {13, 8}, {13, 8}, {13, 8},
{12, 8}, {12, 8}, {12, 8}, {12, 8},
{12, 8}, {12, 8}, {12, 8}, {12, 8},
{11, 8}, {11, 8}, {11, 8}, {11, 8},
{11, 8}, {11, 8}, {11, 8}, {11, 8},
{10, 8}, {10, 8}, {10, 8}, {10, 8},
{10, 8}, {10, 8}, {10, 8}, {10, 8},
{ 9, 8}, { 9, 8}, { 9, 8}, { 9, 8},
{ 9, 8}, { 9, 8}, { 9, 8}, { 9, 8},
{ 8, 7}, { 8, 7}, { 8, 7}, { 8, 7},
{ 8, 7}, { 8, 7}, { 8, 7}, { 8, 7},
{ 8, 7}, { 8, 7}, { 8, 7}, { 8, 7},
{ 8, 7}, { 8, 7}, { 8, 7}, { 8, 7},
{ 7, 7}, { 7, 7}, { 7, 7}, { 7, 7},
{ 7, 7}, { 7, 7}, { 7, 7}, { 7, 7},
{ 7, 7}, { 7, 7}, { 7, 7}, { 7, 7},
{ 7, 7}, { 7, 7}, { 7, 7}, { 7, 7}
};
/* vlc.h, variable length code tables (used by routines in putvlc.c) */
/* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
/*
* Disclaimer of Warranty
*
* These software programs are available to the user without any license fee or
* royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
* any and all warranties, whether express, implied, or statuary, including any
* implied warranties or merchantability or of fitness for a particular
* purpose. In no event shall the copyright-holder be liable for any
* incidental, punitive, or consequential damages of any kind whatsoever
* arising from the use of these programs.
*
* This disclaimer of warranty extends to the user of these programs and user's
* customers, employees, agents, transferees, successors, and assigns.
*
* The MPEG Software Simulation Group does not represent or warrant that the
* programs furnished hereunder are free of infringement of any third-party
* patents.
*
* Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
* are subject to royalty fees to patent holders. Many of these patents are
* general enough such that they are unavoidable regardless of implementation
* design.
*
*/
/* type definitions for variable length code table entries */
typedef struct
{
unsigned char code; /* right justified */
char len;
} VLCtable;
/* for codes longer than 8 bits (excluding leading zeroes) */
typedef struct
{
unsigned short code; /* right justified */
char len;
} sVLCtable;
/* data from ISO/IEC 13818-2 DIS, Annex B, variable length code tables */
/* Table B-1, variable length codes for macroblock_address_increment
*
* indexed by [macroblock_address_increment-1]
* 'macroblock_escape' is treated elsewhere
*/
static const VLCtable addrinctab[33]=
{
{0x01,1}, {0x03,3}, {0x02,3}, {0x03,4},
{0x02,4}, {0x03,5}, {0x02,5}, {0x07,7},
{0x06,7}, {0x0b,8}, {0x0a,8}, {0x09,8},
{0x08,8}, {0x07,8}, {0x06,8}, {0x17,10},
{0x16,10}, {0x15,10}, {0x14,10}, {0x13,10},
{0x12,10}, {0x23,11}, {0x22,11}, {0x21,11},
{0x20,11}, {0x1f,11}, {0x1e,11}, {0x1d,11},
{0x1c,11}, {0x1b,11}, {0x1a,11}, {0x19,11},
{0x18,11}
};
/* Table B-2, B-3, B-4 variable length codes for macroblock_type
*
* indexed by [macroblock_type]
*/
static const VLCtable mbtypetab[3][32]=
{
/* I */
{
{0,0}, {1,1}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
{0,0}, {1,2}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}
},
/* P */
{
{0,0}, {3,5}, {1,2}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
{1,3}, {0,0}, {1,1}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
{0,0}, {1,6}, {1,5}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
{0,0}, {0,0}, {2,5}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}
},
/* B */
{
{0,0}, {3,5}, {0,0}, {0,0}, {2,3}, {0,0}, {3,3}, {0,0},
{2,4}, {0,0}, {3,4}, {0,0}, {2,2}, {0,0}, {3,2}, {0,0},
{0,0}, {1,6}, {0,0}, {0,0}, {0,0}, {0,0}, {2,6}, {0,0},
{0,0}, {0,0}, {3,6}, {0,0}, {0,0}, {0,0}, {2,5}, {0,0}
}
};
/* Table B-5 ... B-8 variable length codes for macroblock_type in
* scalable sequences
*
* not implemented
*/
/* Table B-9, variable length codes for coded_block_pattern
*
* indexed by [coded_block_pattern]
*/
static const VLCtable cbptable[64]=
{
{0x01,9}, {0x0b,5}, {0x09,5}, {0x0d,6},
{0x0d,4}, {0x17,7}, {0x13,7}, {0x1f,8},
{0x0c,4}, {0x16,7}, {0x12,7}, {0x1e,8},
{0x13,5}, {0x1b,8}, {0x17,8}, {0x13,8},
{0x0b,4}, {0x15,7}, {0x11,7}, {0x1d,8},
{0x11,5}, {0x19,8}, {0x15,8}, {0x11,8},
{0x0f,6}, {0x0f,8}, {0x0d,8}, {0x03,9},
{0x0f,5}, {0x0b,8}, {0x07,8}, {0x07,9},
{0x0a,4}, {0x14,7}, {0x10,7}, {0x1c,8},
{0x0e,6}, {0x0e,8}, {0x0c,8}, {0x02,9},
{0x10,5}, {0x18,8}, {0x14,8}, {0x10,8},
{0x0e,5}, {0x0a,8}, {0x06,8}, {0x06,9},
{0x12,5}, {0x1a,8}, {0x16,8}, {0x12,8},
{0x0d,5}, {0x09,8}, {0x05,8}, {0x05,9},
{0x0c,5}, {0x08,8}, {0x04,8}, {0x04,9},
{0x07,3}, {0x0a,5}, {0x08,5}, {0x0c,6}
};
/* Table B-14, DCT coefficients table zero
*
* indexed by [run][level-1]
* split into two tables (dct_code_tab1, dct_code_tab2) to reduce size
* 'first DCT coefficient' condition and 'End of Block' are treated elsewhere
* codes do not include s (sign bit)
*/
static const VLCtable dct_code_tab1[2][40]=
{
/* run = 0, level = 1...40 */
{
{0x03, 2}, {0x04, 4}, {0x05, 5}, {0x06, 7},
{0x26, 8}, {0x21, 8}, {0x0a,10}, {0x1d,12},
{0x18,12}, {0x13,12}, {0x10,12}, {0x1a,13},
{0x19,13}, {0x18,13}, {0x17,13}, {0x1f,14},
{0x1e,14}, {0x1d,14}, {0x1c,14}, {0x1b,14},
{0x1a,14}, {0x19,14}, {0x18,14}, {0x17,14},
{0x16,14}, {0x15,14}, {0x14,14}, {0x13,14},
{0x12,14}, {0x11,14}, {0x10,14}, {0x18,15},
{0x17,15}, {0x16,15}, {0x15,15}, {0x14,15},
{0x13,15}, {0x12,15}, {0x11,15}, {0x10,15}
},
/* run = 1, level = 1...18 */
{
{0x03, 3}, {0x06, 6}, {0x25, 8}, {0x0c,10},
{0x1b,12}, {0x16,13}, {0x15,13}, {0x1f,15},
{0x1e,15}, {0x1d,15}, {0x1c,15}, {0x1b,15},
{0x1a,15}, {0x19,15}, {0x13,16}, {0x12,16},
{0x11,16}, {0x10,16}, {0x00, 0}, {0x00, 0},
{0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0},
{0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0},
{0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0},
{0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0},
{0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}
}
};
static const VLCtable dct_code_tab2[30][5]=
{
/* run = 2...31, level = 1...5 */
{{0x05, 4}, {0x04, 7}, {0x0b,10}, {0x14,12}, {0x14,13}},
{{0x07, 5}, {0x24, 8}, {0x1c,12}, {0x13,13}, {0x00, 0}},
{{0x06, 5}, {0x0f,10}, {0x12,12}, {0x00, 0}, {0x00, 0}},
{{0x07, 6}, {0x09,10}, {0x12,13}, {0x00, 0}, {0x00, 0}},
{{0x05, 6}, {0x1e,12}, {0x14,16}, {0x00, 0}, {0x00, 0}},
{{0x04, 6}, {0x15,12}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x07, 7}, {0x11,12}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x05, 7}, {0x11,13}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x27, 8}, {0x10,13}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x23, 8}, {0x1a,16}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x22, 8}, {0x19,16}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x20, 8}, {0x18,16}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x0e,10}, {0x17,16}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x0d,10}, {0x16,16}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x08,10}, {0x15,16}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x1f,12}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x1a,12}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x19,12}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x17,12}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x16,12}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x1f,13}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x1e,13}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x1d,13}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x1c,13}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x1b,13}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x1f,16}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x1e,16}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x1d,16}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x1c,16}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x1b,16}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}}
};
/* Table B-15, DCT coefficients table one
*
* indexed by [run][level-1]
* split into two tables (dct_code_tab1a, dct_code_tab2a) to reduce size
* 'End of Block' is treated elsewhere
* codes do not include s (sign bit)
*/
static const VLCtable dct_code_tab1a[2][40]=
{
/* run = 0, level = 1...40 */
{
{0x02, 2}, {0x06, 3}, {0x07, 4}, {0x1c, 5},
{0x1d, 5}, {0x05, 6}, {0x04, 6}, {0x7b, 7},
{0x7c, 7}, {0x23, 8}, {0x22, 8}, {0xfa, 8},
{0xfb, 8}, {0xfe, 8}, {0xff, 8}, {0x1f,14},
{0x1e,14}, {0x1d,14}, {0x1c,14}, {0x1b,14},
{0x1a,14}, {0x19,14}, {0x18,14}, {0x17,14},
{0x16,14}, {0x15,14}, {0x14,14}, {0x13,14},
{0x12,14}, {0x11,14}, {0x10,14}, {0x18,15},
{0x17,15}, {0x16,15}, {0x15,15}, {0x14,15},
{0x13,15}, {0x12,15}, {0x11,15}, {0x10,15}
},
/* run = 1, level = 1...18 */
{
{0x02, 3}, {0x06, 5}, {0x79, 7}, {0x27, 8},
{0x20, 8}, {0x16,13}, {0x15,13}, {0x1f,15},
{0x1e,15}, {0x1d,15}, {0x1c,15}, {0x1b,15},
{0x1a,15}, {0x19,15}, {0x13,16}, {0x12,16},
{0x11,16}, {0x10,16}, {0x00, 0}, {0x00, 0},
{0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0},
{0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0},
{0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0},
{0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0},
{0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}
}
};
static const VLCtable dct_code_tab2a[30][5]=
{
/* run = 2...31, level = 1...5 */
{{0x05, 5}, {0x07, 7}, {0xfc, 8}, {0x0c,10}, {0x14,13}},
{{0x07, 5}, {0x26, 8}, {0x1c,12}, {0x13,13}, {0x00, 0}},
{{0x06, 6}, {0xfd, 8}, {0x12,12}, {0x00, 0}, {0x00, 0}},
{{0x07, 6}, {0x04, 9}, {0x12,13}, {0x00, 0}, {0x00, 0}},
{{0x06, 7}, {0x1e,12}, {0x14,16}, {0x00, 0}, {0x00, 0}},
{{0x04, 7}, {0x15,12}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x05, 7}, {0x11,12}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x78, 7}, {0x11,13}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x7a, 7}, {0x10,13}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x21, 8}, {0x1a,16}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x25, 8}, {0x19,16}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x24, 8}, {0x18,16}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x05, 9}, {0x17,16}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x07, 9}, {0x16,16}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x0d,10}, {0x15,16}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x1f,12}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x1a,12}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x19,12}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x17,12}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x16,12}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x1f,13}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x1e,13}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x1d,13}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x1c,13}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x1b,13}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x1f,16}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x1e,16}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x1d,16}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x1c,16}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}},
{{0x1b,16}, {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}}
};
/* MPEG-4 matrices */
static const uint8_t mpeg4_default_intra_matrix[64] = {
8, 17, 18, 19, 21, 23, 25, 27,
17, 18, 19, 21, 23, 25, 27, 28,
20, 21, 22, 23, 24, 26, 28, 30,
21, 22, 23, 24, 26, 28, 30, 32,
22, 23, 24, 26, 28, 30, 32, 35,
23, 24, 26, 28, 30, 32, 35, 38,
25, 26, 28, 30, 32, 35, 38, 41,
27, 28, 30, 32, 35, 38, 41, 45,
};
static const uint8_t mpeg4_default_non_intra_matrix[64] = {
16, 17, 18, 19, 20, 21, 22, 23,
17, 18, 19, 20, 21, 22, 23, 24,
18, 19, 20, 21, 22, 23, 24, 25,
19, 20, 21, 22, 23, 24, 26, 27,
20, 21, 22, 23, 25, 26, 27, 28,
21, 22, 23, 24, 26, 27, 28, 30,
22, 23, 24, 26, 27, 28, 30, 31,
23, 24, 25, 27, 28, 30, 31, 33,
};
/*****************************************************************************
* transrate.c: MPEG2 video transrating module
*****************************************************************************
* Copyright (C) 2003 the VideoLAN team
* $Id$
*
* Authors: Christophe Massiot <massiot@via.ecp.fr>
* Laurent Aimar <fenrir@via.ecp.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
#define NDEBUG 1
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include <math.h>
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_sout.h>
#include <vlc_input.h>
#include <vlc_block.h>
#include "transrate.h"
/*****************************************************************************
* Exported prototypes
*****************************************************************************/
static int Open ( vlc_object_t * );
static void Close ( vlc_object_t * );
static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
static int Del ( sout_stream_t *, sout_stream_id_t * );
static int Send( sout_stream_t *, sout_stream_id_t *, block_t * );
static int transrate_video_process( sout_stream_t *, sout_stream_id_t *, block_t *, block_t ** );
/*****************************************************************************
* Module descriptor
*****************************************************************************/
#define VB_TEXT N_("Video bitrate")
/*xgettext:no-c-format*/
#define VB_LONGTEXT N_( \
"New target video bitrate. Quality is ok for -10/15\% of the original" \
"bitrate." )
#define SHAPING_TEXT N_("Shaping delay")
#define SHAPING_LONGTEXT N_( \
"Amount of data used for transrating in ms." )
#define MPEG4_MATRIX_TEXT N_("Use MPEG4 matrix")
#define MPEG4_MATRIX_LONGTEXT N_( \
"Use the MPEG4 quantification matrix." )
#define SOUT_CFG_PREFIX "sout-transrate-"
vlc_module_begin ()
set_category( CAT_SOUT )
set_subcategory( SUBCAT_SOUT_STREAM )
set_description( N_("MPEG2 video transrating stream output") )
set_capability( "sout stream", 50 )
add_shortcut( "transrate" )
set_shortname( N_("Transrate") )
set_callbacks( Open, Close )
add_integer( SOUT_CFG_PREFIX "vb", 3 * 100 * 1000, NULL,
VB_TEXT, VB_LONGTEXT, false )
add_integer( SOUT_CFG_PREFIX "shaping", 500, NULL,
SHAPING_TEXT, SHAPING_LONGTEXT, false )
add_bool( SOUT_CFG_PREFIX "mpeg4-matrix", false, NULL,
MPEG4_MATRIX_TEXT, MPEG4_MATRIX_LONGTEXT, false )
vlc_module_end ()
static const char *const ppsz_sout_options[] = {
"vb", "shaping", "mpeg4-matrix", NULL
};
struct sout_stream_sys_t
{
sout_stream_t *p_out;
int i_vbitrate;
mtime_t i_shaping_delay;
int b_mpeg4_matrix;
mtime_t i_dts, i_pts;
};
/*****************************************************************************
* Open:
*****************************************************************************/
static int Open( vlc_object_t *p_this )
{
sout_stream_t *p_stream = (sout_stream_t*)p_this;
sout_stream_sys_t *p_sys;
p_sys = malloc( sizeof( sout_stream_sys_t ) );
p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
p_stream->p_cfg );
p_sys->i_vbitrate = var_CreateGetInteger( p_stream, SOUT_CFG_PREFIX "vb" );
if( p_sys->i_vbitrate < 16000 )
p_sys->i_vbitrate *= 1000;
p_sys->i_shaping_delay = var_CreateGetInteger( p_stream,
SOUT_CFG_PREFIX "shaping" ) * 1000;
if( p_sys->i_shaping_delay <= 0 )
{
msg_Err( p_stream,
"invalid shaping (%"PRId64"ms) reseting to 500ms",
p_sys->i_shaping_delay / 1000 );
p_sys->i_shaping_delay = 500000;
}
p_sys->b_mpeg4_matrix = var_CreateGetBool( p_stream,
SOUT_CFG_PREFIX "mpeg4-matrix" );
msg_Dbg( p_stream, "codec video %dkb/s max gop=%"PRId64"us",
p_sys->i_vbitrate / 1024, p_sys->i_shaping_delay );
if( !p_sys->p_out )
{
msg_Err( p_stream, "cannot create chain" );
free( p_sys );
return VLC_EGENERIC;
}
p_stream->pf_add = Add;
p_stream->pf_del = Del;
p_stream->pf_send = Send;
p_stream->p_sys = p_sys;
return VLC_SUCCESS;
}
/*****************************************************************************
* Close:
*****************************************************************************/
static void Close( vlc_object_t * p_this )
{
sout_stream_t *p_stream = (sout_stream_t *)p_this;
sout_stream_sys_t *p_sys = p_stream->p_sys;
sout_StreamDelete( p_sys->p_out );
free( p_sys );
}
static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
{
sout_stream_sys_t *p_sys = p_stream->p_sys;
sout_stream_id_t *id;
id = malloc( sizeof( sout_stream_id_t ) );
id->id = NULL;
if( p_fmt->i_cat == VIDEO_ES
&& p_fmt->i_codec == VLC_CODEC_MPGV )
{
msg_Dbg( p_stream,
"creating video transrating for fcc=`%4.4s'",
(char*)&p_fmt->i_codec );
id->p_current_buffer = NULL;
id->p_next_gop = NULL;
id->i_next_gop_duration = 0;
id->i_next_gop_size = 0;
memset( &id->tr, 0, sizeof( transrate_t ) );
id->tr.bs.i_byte_in = id->tr.bs.i_byte_out = 0;
id->tr.mpeg4_matrix = p_sys->b_mpeg4_matrix;
/* open output stream */
id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
id->b_transrate = true;
}
else
{
msg_Dbg( p_stream, "not transrating a stream (fcc=`%4.4s')", (char*)&p_fmt->i_codec );
id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
id->b_transrate = false;
if( id->id == NULL )
{
free( id );
return NULL;
}
}
return id;
}
static int Del ( sout_stream_t *p_stream, sout_stream_id_t *id )
{
sout_stream_sys_t *p_sys = p_stream->p_sys;
if( id->id )
{
p_sys->p_out->pf_del( p_sys->p_out, id->id );
}
free( id );
return VLC_SUCCESS;
}
static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
block_t *p_buffer )
{
sout_stream_sys_t *p_sys = p_stream->p_sys;
if( id->b_transrate )
{
block_t *p_buffer_out;
/* be sure to have at least 8 bytes of padding (maybe only 4) */
p_buffer = block_Realloc( p_buffer, 0, p_buffer->i_buffer + 8 );
p_buffer->i_buffer -= 8;
memset( &p_buffer->p_buffer[p_buffer->i_buffer], 0, 8 );
transrate_video_process( p_stream, id, p_buffer, &p_buffer_out );
if( p_buffer_out )
{
return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
}
return VLC_SUCCESS;
}
else if( id->id != NULL )
{
return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
}
else
{
block_Release( p_buffer );
return VLC_EGENERIC;
}
}
static int transrate_video_process( sout_stream_t *p_stream,
sout_stream_id_t *id, block_t *in, block_t **out )
{
transrate_t *tr = &id->tr;
bs_transrate_t *bs = &tr->bs;
*out = NULL;
while ( in != NULL )
{
block_t * p_next = in->p_next;
int i_flags = in->i_flags;
in->p_next = NULL;
block_ChainAppend( &id->p_next_gop, in );
id->i_next_gop_duration += in->i_length;
id->i_next_gop_size += in->i_buffer;
in = p_next;
if( ((i_flags & BLOCK_FLAG_TYPE_I )
&& id->i_next_gop_duration >= 300000)
|| (id->i_next_gop_duration > p_stream->p_sys->i_shaping_delay) )
{
mtime_t i_bitrate = (mtime_t)id->i_next_gop_size * 8000
/ (id->i_next_gop_duration / 1000);
mtime_t i_new_bitrate;
id->tr.i_total_input = id->i_next_gop_size;
id->tr.i_remaining_input = id->i_next_gop_size;
id->tr.i_wanted_output = (p_stream->p_sys->i_vbitrate)
* (id->i_next_gop_duration / 1000) / 8000;
id->tr.i_current_output = 0;
id->p_current_buffer = id->p_next_gop;
while ( id->p_current_buffer != NULL )
{
block_t * p_next = id->p_current_buffer->p_next;
if ( !p_stream->p_sys->b_mpeg4_matrix
&& id->tr.i_wanted_output >= id->tr.i_total_input )
{
bs->i_byte_out += id->p_current_buffer->i_buffer;
id->p_current_buffer->p_next = NULL;
block_ChainAppend( out, id->p_current_buffer );
}
else
{
if ( process_frame( p_stream, id, id->p_current_buffer,
out, 0 ) < 0 )
{
id->p_current_buffer->p_next = NULL;
block_ChainAppend( out, id->p_current_buffer );
if ( p_stream->p_sys->b_mpeg4_matrix )
id->tr.i_wanted_output = id->tr.i_total_input;
}
else
{
block_Release( id->p_current_buffer );
}
}
id->p_current_buffer = p_next;
}
if ( id->tr.i_wanted_output < id->tr.i_total_input )
{
i_new_bitrate = (mtime_t)tr->i_current_output * 8000
/ (id->i_next_gop_duration / 1000);
if (i_new_bitrate > p_stream->p_sys->i_vbitrate + 300000)
msg_Err(p_stream, "%"PRId64" -> %"PRId64" d=%"PRId64,
i_bitrate, i_new_bitrate,
id->i_next_gop_duration);
else
msg_Dbg(p_stream, "%"PRId64" -> %"PRId64" d=%"PRId64,
i_bitrate, i_new_bitrate,
id->i_next_gop_duration);
}
id->p_next_gop = NULL;
id->i_next_gop_duration = 0;
id->i_next_gop_size = 0;
}
}
return VLC_SUCCESS;
}
/*****************************************************************************
* transrate.h: MPEG2 video transrating module
*****************************************************************************
* Copyright (C) 2003 the VideoLAN team
* Copyright (C) 2003 Antoine Missout
* Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
* Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
* $Id$
*
* Authors: Christophe Massiot <massiot@via.ecp.fr>
* Laurent Aimar <fenrir@via.ecp.fr>
* Antoine Missout
* Michel Lespinasse <walken@zoy.org>
* Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/*****************************************************************************
* sout_stream_id_t:
*****************************************************************************/
typedef struct
{
uint8_t run;
short level;
} RunLevel;
typedef struct
{
uint8_t *p_c;
uint8_t *p_r;
uint8_t *p_w;
uint8_t *p_ow;
uint8_t *p_rw;
int i_bit_in;
int i_bit_out;
uint32_t i_bit_in_cache;
uint32_t i_bit_out_cache;
uint32_t i_byte_in;
uint32_t i_byte_out;
} bs_transrate_t;
typedef struct
{
bs_transrate_t bs;
/* MPEG2 state */
// seq header
unsigned int horizontal_size_value;
unsigned int vertical_size_value;
uint8_t intra_quantizer_matrix [64];
uint8_t non_intra_quantizer_matrix [64];
int mpeg4_matrix;
// pic header
unsigned int picture_coding_type;
// pic code ext
unsigned int f_code[2][2];
/* unsigned int intra_dc_precision; */
unsigned int picture_structure;
unsigned int frame_pred_frame_dct;
unsigned int concealment_motion_vectors;
unsigned int q_scale_type;
unsigned int intra_vlc_format;
const uint8_t * scan;
// slice or mb
// quantizer_scale_code
unsigned int quantizer_scale;
unsigned int new_quantizer_scale;
unsigned int last_coded_scale;
int h_offset, v_offset;
bool b_error;
// mb
double qrate;
int i_admissible_error, i_minimum_error;
/* input buffers */
ssize_t i_total_input, i_remaining_input;
/* output buffers */
ssize_t i_current_output, i_wanted_output;
} transrate_t;
struct sout_stream_id_t
{
void *id;
bool b_transrate;
block_t *p_current_buffer;
block_t *p_next_gop;
mtime_t i_next_gop_duration;
size_t i_next_gop_size;
transrate_t tr;
};
#ifdef HAVE_BUILTIN_EXPECT
#define likely(x) __builtin_expect ((x) != 0, 1)
#define unlikely(x) __builtin_expect ((x) != 0, 0)
#else
#define likely(x) (x)
#define unlikely(x) (x)
#endif
#define BITS_IN_BUF (8)
#define LOG(msg) fprintf (stderr, msg)
#define LOGF(format, args...) fprintf (stderr, format, args)
static inline void bs_write( bs_transrate_t *s, unsigned int val, int n )
{
assert(n < 32);
assert(!(val & (0xffffffffU << n)));
while (unlikely(n >= s->i_bit_out))
{
s->p_w[0] = (s->i_bit_out_cache << s->i_bit_out ) | (val >> (n - s->i_bit_out));
s->p_w++;
n -= s->i_bit_out;
s->i_bit_out_cache = 0;
val &= ~(0xffffffffU << n);
s->i_bit_out = BITS_IN_BUF;
}
if (likely(n))
{
s->i_bit_out_cache = (s->i_bit_out_cache << n) | val;
s->i_bit_out -= n;
}
assert(s->i_bit_out > 0);
assert(s->i_bit_out <= BITS_IN_BUF);
}
static inline void bs_refill( bs_transrate_t *s )
{
assert((s->p_r - s->p_c) >= 1);
s->i_bit_in_cache |= s->p_c[0] << (24 - s->i_bit_in);
s->i_bit_in += 8;
s->p_c++;
}
static inline void bs_flush( bs_transrate_t *s, unsigned int n )
{
assert(s->i_bit_in >= n);
s->i_bit_in_cache <<= n;
s->i_bit_in -= n;
assert( (!n) || ((n>0) && !(s->i_bit_in_cache & 0x1)) );
while (unlikely(s->i_bit_in < 24)) bs_refill( s );
}
static inline unsigned int bs_read( bs_transrate_t *s, unsigned int n )
{
unsigned int Val = ((unsigned int)s->i_bit_in_cache) >> (32 - n);
bs_flush( s, n );
return Val;
}
static inline unsigned int bs_copy( bs_transrate_t *s, unsigned int n )
{
unsigned int Val = bs_read( s, n);
bs_write(s, Val, n);
return Val;
}
static inline void bs_flush_read( bs_transrate_t *s )
{
int i = s->i_bit_in & 0x7;
if( i )
{
assert(((unsigned int)s->i_bit_in_cache) >> (32 - i) == 0);
s->i_bit_in_cache <<= i;
s->i_bit_in -= i;
}
s->p_c += -1 * (s->i_bit_in >> 3);
s->i_bit_in = 0;
}
static inline void bs_flush_write( bs_transrate_t *s )
{
if( s->i_bit_out != 8 ) bs_write(s, 0, s->i_bit_out);
}
int scale_quant( transrate_t *tr, double qrate );
int transrate_mb( transrate_t *tr, RunLevel blk[6][65], RunLevel new_blk[6][65], int i_cbp, int intra );
void get_intra_block_B14( transrate_t *tr, RunLevel *blk );
void get_intra_block_B15( transrate_t *tr, RunLevel *blk );
int get_non_intra_block( transrate_t *tr, RunLevel *blk );
void putnonintrablk( bs_transrate_t *bs, RunLevel *blk);
void putintrablk( bs_transrate_t *bs, RunLevel *blk, int vlcformat);
int process_frame( sout_stream_t *p_stream, sout_stream_id_t *id,
block_t *in, block_t **out, int i_handicap );
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment