Commit 20390e31 authored by Diego Biurrun's avatar Diego Biurrun

cosmetics: Move MPEG-4 encoding functions around to avoid forward declarations.

Originally committed as revision 20981 to svn://svn.ffmpeg.org/ffmpeg/trunk
parent df8d98c4
......@@ -57,16 +57,6 @@
#define CBPC_B_VLC_BITS 3
#if CONFIG_ENCODERS
static void mpeg4_encode_visual_object_header(MpegEncContext *s);
static void mpeg4_encode_vol_header(MpegEncContext *s, int vo_number,
int vol_number);
static inline void mpeg4_encode_block(MpegEncContext *s, DCTELEM *block,
int n, int dc, uint8_t *scan_table,
PutBitContext *dc_pb,
PutBitContext *ac_pb);
static int mpeg4_get_block_length(MpegEncContext *s, DCTELEM *block, int n,
int intra_dc, uint8_t *scan_table);
static uint8_t uni_DCtab_lum_len[512];
static uint8_t uni_DCtab_chrom_len[512];
static uint16_t uni_DCtab_lum_bits[512];
......@@ -788,6 +778,229 @@ static inline int ff_mpeg4_pred_dc(MpegEncContext * s, int n, int level, int *di
#if CONFIG_ENCODERS
/**
* encodes the dc value.
* @param n block index (0-3 are luma, 4-5 are chroma)
*/
static inline void mpeg4_encode_dc(PutBitContext * s, int level, int n)
{
#if 1
level+=256;
if (n < 4) {
/* luminance */
put_bits(s, uni_DCtab_lum_len[level], uni_DCtab_lum_bits[level]);
} else {
/* chrominance */
put_bits(s, uni_DCtab_chrom_len[level], uni_DCtab_chrom_bits[level]);
}
#else
int size, v;
/* find number of bits */
size = 0;
v = abs(level);
while (v) {
v >>= 1;
size++;
}
if (n < 4) {
/* luminance */
put_bits(&s->pb, DCtab_lum[size][1], DCtab_lum[size][0]);
} else {
/* chrominance */
put_bits(&s->pb, DCtab_chrom[size][1], DCtab_chrom[size][0]);
}
/* encode remaining bits */
if (size > 0) {
if (level < 0)
level = (-level) ^ ((1 << size) - 1);
put_bits(&s->pb, size, level);
if (size > 8)
put_bits(&s->pb, 1, 1);
}
#endif
}
static inline int mpeg4_get_dc_length(int level, int n){
if (n < 4) {
return uni_DCtab_lum_len[level + 256];
} else {
return uni_DCtab_chrom_len[level + 256];
}
}
/**
* encodes a 8x8 block
* @param n block index (0-3 are luma, 4-5 are chroma)
*/
static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n, int intra_dc,
uint8_t *scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb)
{
int i, last_non_zero;
#if 0 //variables for the outcommented version
int code, sign, last;
#endif
const RLTable *rl;
uint32_t *bits_tab;
uint8_t *len_tab;
const int last_index = s->block_last_index[n];
if (s->mb_intra) { //Note gcc (3.2.1 at least) will optimize this away
/* mpeg4 based DC predictor */
mpeg4_encode_dc(dc_pb, intra_dc, n);
if(last_index<1) return;
i = 1;
rl = &rl_intra;
bits_tab= uni_mpeg4_intra_rl_bits;
len_tab = uni_mpeg4_intra_rl_len;
} else {
if(last_index<0) return;
i = 0;
rl = &rl_inter;
bits_tab= uni_mpeg4_inter_rl_bits;
len_tab = uni_mpeg4_inter_rl_len;
}
/* AC coefs */
last_non_zero = i - 1;
#if 1
for (; i < last_index; i++) {
int level = block[ scan_table[i] ];
if (level) {
int run = i - last_non_zero - 1;
level+=64;
if((level&(~127)) == 0){
const int index= UNI_MPEG4_ENC_INDEX(0, run, level);
put_bits(ac_pb, len_tab[index], bits_tab[index]);
}else{ //ESC3
put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(0<<20)+(run<<14)+(1<<13)+(((level-64)&0xfff)<<1)+1);
}
last_non_zero = i;
}
}
/*if(i<=last_index)*/{
int level = block[ scan_table[i] ];
int run = i - last_non_zero - 1;
level+=64;
if((level&(~127)) == 0){
const int index= UNI_MPEG4_ENC_INDEX(1, run, level);
put_bits(ac_pb, len_tab[index], bits_tab[index]);
}else{ //ESC3
put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(1<<20)+(run<<14)+(1<<13)+(((level-64)&0xfff)<<1)+1);
}
}
#else
for (; i <= last_index; i++) {
const int slevel = block[ scan_table[i] ];
if (slevel) {
int level;
int run = i - last_non_zero - 1;
last = (i == last_index);
sign = 0;
level = slevel;
if (level < 0) {
sign = 1;
level = -level;
}
code = get_rl_index(rl, last, run, level);
put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
if (code == rl->n) {
int level1, run1;
level1 = level - rl->max_level[last][run];
if (level1 < 1)
goto esc2;
code = get_rl_index(rl, last, run, level1);
if (code == rl->n) {
esc2:
put_bits(ac_pb, 1, 1);
if (level > MAX_LEVEL)
goto esc3;
run1 = run - rl->max_run[last][level] - 1;
if (run1 < 0)
goto esc3;
code = get_rl_index(rl, last, run1, level);
if (code == rl->n) {
esc3:
/* third escape */
put_bits(ac_pb, 1, 1);
put_bits(ac_pb, 1, last);
put_bits(ac_pb, 6, run);
put_bits(ac_pb, 1, 1);
put_sbits(ac_pb, 12, slevel);
put_bits(ac_pb, 1, 1);
} else {
/* second escape */
put_bits(ac_pb, 1, 0);
put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
put_bits(ac_pb, 1, sign);
}
} else {
/* first escape */
put_bits(ac_pb, 1, 0);
put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
put_bits(ac_pb, 1, sign);
}
} else {
put_bits(ac_pb, 1, sign);
}
last_non_zero = i;
}
}
#endif
}
static int mpeg4_get_block_length(MpegEncContext * s, DCTELEM * block, int n, int intra_dc,
uint8_t *scan_table)
{
int i, last_non_zero;
uint8_t *len_tab;
const int last_index = s->block_last_index[n];
int len=0;
if (s->mb_intra) { //Note gcc (3.2.1 at least) will optimize this away
/* mpeg4 based DC predictor */
len += mpeg4_get_dc_length(intra_dc, n);
if(last_index<1) return len;
i = 1;
len_tab = uni_mpeg4_intra_rl_len;
} else {
if(last_index<0) return 0;
i = 0;
len_tab = uni_mpeg4_inter_rl_len;
}
/* AC coefs */
last_non_zero = i - 1;
for (; i < last_index; i++) {
int level = block[ scan_table[i] ];
if (level) {
int run = i - last_non_zero - 1;
level+=64;
if((level&(~127)) == 0){
const int index= UNI_MPEG4_ENC_INDEX(0, run, level);
len += len_tab[index];
}else{ //ESC3
len += 7+2+1+6+1+12+1;
}
last_non_zero = i;
}
}
/*if(i<=last_index)*/{
int level = block[ scan_table[i] ];
int run = i - last_non_zero - 1;
level+=64;
if((level&(~127)) == 0){
const int index= UNI_MPEG4_ENC_INDEX(1, run, level);
len += len_tab[index];
}else{ //ESC3
len += 7+2+1+6+1+12+1;
}
}
return len;
}
static inline int h263_get_motion_length(MpegEncContext * s, int val, int f_code){
int l, bit_size, code;
......@@ -1950,600 +2163,600 @@ int16_t *h263_pred_motion(MpegEncContext * s, int block, int dir,
}
#if CONFIG_ENCODERS
void ff_h263_encode_motion(MpegEncContext * s, int val, int f_code)
{
int range, l, bit_size, sign, code, bits;
if (val == 0) {
/* zero vector */
code = 0;
put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
} else {
bit_size = f_code - 1;
range = 1 << bit_size;
/* modulo encoding */
l= INT_BIT - 6 - bit_size;
val = (val<<l)>>l;
sign = val>>31;
val= (val^sign)-sign;
sign&=1;
val--;
code = (val >> bit_size) + 1;
bits = val & (range - 1);
/***************************************************/
/**
* add mpeg4 stuffing bits (01...1)
*/
void ff_mpeg4_stuffing(PutBitContext * pbc)
{
int length;
put_bits(pbc, 1, 0);
length= (-put_bits_count(pbc))&7;
if(length) put_bits(pbc, length, (1<<length)-1);
}
put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
if (bit_size > 0) {
put_bits(&s->pb, bit_size, bits);
}
/* must be called before writing the header */
void ff_set_mpeg4_time(MpegEncContext * s){
if(s->pict_type==FF_B_TYPE){
ff_mpeg4_init_direct_mv(s);
}else{
s->last_time_base= s->time_base;
s->time_base= s->time/s->avctx->time_base.den;
}
}
static void init_mv_penalty_and_fcode(MpegEncContext *s)
{
int f_code;
int mv;
static void mpeg4_encode_gop_header(MpegEncContext * s){
int hours, minutes, seconds;
int64_t time;
for(f_code=1; f_code<=MAX_FCODE; f_code++){
for(mv=-MAX_MV; mv<=MAX_MV; mv++){
int len;
put_bits(&s->pb, 16, 0);
put_bits(&s->pb, 16, GOP_STARTCODE);
if(mv==0) len= mvtab[0][1];
else{
int val, bit_size, code;
time= s->current_picture_ptr->pts;
if(s->reordered_input_picture[1])
time= FFMIN(time, s->reordered_input_picture[1]->pts);
time= time*s->avctx->time_base.num;
bit_size = f_code - 1;
seconds= time/s->avctx->time_base.den;
minutes= seconds/60; seconds %= 60;
hours= minutes/60; minutes %= 60;
hours%=24;
val=mv;
if (val < 0)
val = -val;
val--;
code = (val >> bit_size) + 1;
if(code<33){
len= mvtab[code][1] + 1 + bit_size;
}else{
len= mvtab[32][1] + av_log2(code>>5) + 2 + bit_size;
}
}
put_bits(&s->pb, 5, hours);
put_bits(&s->pb, 6, minutes);
put_bits(&s->pb, 1, 1);
put_bits(&s->pb, 6, seconds);
mv_penalty[f_code][mv+MAX_MV]= len;
}
put_bits(&s->pb, 1, !!(s->flags&CODEC_FLAG_CLOSED_GOP));
put_bits(&s->pb, 1, 0); //broken link == NO
s->last_time_base= time / s->avctx->time_base.den;
ff_mpeg4_stuffing(&s->pb);
}
static void mpeg4_encode_visual_object_header(MpegEncContext * s){
int profile_and_level_indication;
int vo_ver_id;
if(s->avctx->profile != FF_PROFILE_UNKNOWN){
profile_and_level_indication = s->avctx->profile << 4;
}else if(s->max_b_frames || s->quarter_sample){
profile_and_level_indication= 0xF0; // adv simple
}else{
profile_and_level_indication= 0x00; // simple
}
for(f_code=MAX_FCODE; f_code>0; f_code--){
for(mv=-(16<<f_code); mv<(16<<f_code); mv++){
fcode_tab[mv+MAX_MV]= f_code;
}
if(s->avctx->level != FF_LEVEL_UNKNOWN){
profile_and_level_indication |= s->avctx->level;
}else{
profile_and_level_indication |= 1; //level 1
}
for(mv=0; mv<MAX_MV*2+1; mv++){
umv_fcode_tab[mv]= 1;
if(profile_and_level_indication>>4 == 0xF){
vo_ver_id= 5;
}else{
vo_ver_id= 1;
}
}
static void init_uni_dc_tab(void)
{
int level, uni_code, uni_len;
//FIXME levels
for(level=-256; level<256; level++){
int size, v, l;
/* find number of bits */
size = 0;
v = abs(level);
while (v) {
v >>= 1;
size++;
}
put_bits(&s->pb, 16, 0);
put_bits(&s->pb, 16, VOS_STARTCODE);
if (level < 0)
l= (-level) ^ ((1 << size) - 1);
else
l= level;
put_bits(&s->pb, 8, profile_and_level_indication);
/* luminance */
uni_code= DCtab_lum[size][0];
uni_len = DCtab_lum[size][1];
put_bits(&s->pb, 16, 0);
put_bits(&s->pb, 16, VISUAL_OBJ_STARTCODE);
if (size > 0) {
uni_code<<=size; uni_code|=l;
uni_len+=size;
if (size > 8){
uni_code<<=1; uni_code|=1;
uni_len++;
}
}
uni_DCtab_lum_bits[level+256]= uni_code;
uni_DCtab_lum_len [level+256]= uni_len;
put_bits(&s->pb, 1, 1);
put_bits(&s->pb, 4, vo_ver_id);
put_bits(&s->pb, 3, 1); //priority
/* chrominance */
uni_code= DCtab_chrom[size][0];
uni_len = DCtab_chrom[size][1];
put_bits(&s->pb, 4, 1); //visual obj type== video obj
if (size > 0) {
uni_code<<=size; uni_code|=l;
uni_len+=size;
if (size > 8){
uni_code<<=1; uni_code|=1;
uni_len++;
}
}
uni_DCtab_chrom_bits[level+256]= uni_code;
uni_DCtab_chrom_len [level+256]= uni_len;
put_bits(&s->pb, 1, 0); //video signal type == no clue //FIXME
}
ff_mpeg4_stuffing(&s->pb);
}
static void init_uni_mpeg4_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab){
int slevel, run, last;
assert(MAX_LEVEL >= 64);
assert(MAX_RUN >= 63);
for(slevel=-64; slevel<64; slevel++){
if(slevel==0) continue;
for(run=0; run<64; run++){
for(last=0; last<=1; last++){
const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
int level= slevel < 0 ? -slevel : slevel;
int sign= slevel < 0 ? 1 : 0;
int bits, len, code;
int level1, run1;
static void mpeg4_encode_vol_header(MpegEncContext * s, int vo_number, int vol_number)
{
int vo_ver_id;
len_tab[index]= 100;
if (!CONFIG_MPEG4_ENCODER) return;
/* ESC0 */
code= get_rl_index(rl, last, run, level);
bits= rl->table_vlc[code][0];
len= rl->table_vlc[code][1];
bits=bits*2+sign; len++;
if(s->max_b_frames || s->quarter_sample){
vo_ver_id= 5;
s->vo_type= ADV_SIMPLE_VO_TYPE;
}else{
vo_ver_id= 1;
s->vo_type= SIMPLE_VO_TYPE;
}
if(code!=rl->n && len < len_tab[index]){
bits_tab[index]= bits;
len_tab [index]= len;
}
/* ESC1 */
bits= rl->table_vlc[rl->n][0];
len= rl->table_vlc[rl->n][1];
bits=bits*2; len++; //esc1
level1= level - rl->max_level[last][run];
if(level1>0){
code= get_rl_index(rl, last, run, level1);
bits<<= rl->table_vlc[code][1];
len += rl->table_vlc[code][1];
bits += rl->table_vlc[code][0];
bits=bits*2+sign; len++;
put_bits(&s->pb, 16, 0);
put_bits(&s->pb, 16, 0x100 + vo_number); /* video obj */
put_bits(&s->pb, 16, 0);
put_bits(&s->pb, 16, 0x120 + vol_number); /* video obj layer */
if(code!=rl->n && len < len_tab[index]){
bits_tab[index]= bits;
len_tab [index]= len;
}
}
/* ESC2 */
bits= rl->table_vlc[rl->n][0];
len= rl->table_vlc[rl->n][1];
bits=bits*4+2; len+=2; //esc2
run1 = run - rl->max_run[last][level] - 1;
if(run1>=0){
code= get_rl_index(rl, last, run1, level);
bits<<= rl->table_vlc[code][1];
len += rl->table_vlc[code][1];
bits += rl->table_vlc[code][0];
bits=bits*2+sign; len++;
put_bits(&s->pb, 1, 0); /* random access vol */
put_bits(&s->pb, 8, s->vo_type); /* video obj type indication */
if(s->workaround_bugs & FF_BUG_MS) {
put_bits(&s->pb, 1, 0); /* is obj layer id= no */
} else {
put_bits(&s->pb, 1, 1); /* is obj layer id= yes */
put_bits(&s->pb, 4, vo_ver_id); /* is obj layer ver id */
put_bits(&s->pb, 3, 1); /* is obj layer priority */
}
if(code!=rl->n && len < len_tab[index]){
bits_tab[index]= bits;
len_tab [index]= len;
}
}
/* ESC3 */
bits= rl->table_vlc[rl->n][0];
len = rl->table_vlc[rl->n][1];
bits=bits*4+3; len+=2; //esc3
bits=bits*2+last; len++;
bits=bits*64+run; len+=6;
bits=bits*2+1; len++; //marker
bits=bits*4096+(slevel&0xfff); len+=12;
bits=bits*2+1; len++; //marker
aspect_to_info(s, s->avctx->sample_aspect_ratio);
if(len < len_tab[index]){
bits_tab[index]= bits;
len_tab [index]= len;
}
}
}
put_bits(&s->pb, 4, s->aspect_ratio_info);/* aspect ratio info */
if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){
put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
}
}
static void init_uni_h263_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab){
int slevel, run, last;
if(s->workaround_bugs & FF_BUG_MS) { //
put_bits(&s->pb, 1, 0); /* vol control parameters= no @@@ */
} else {
put_bits(&s->pb, 1, 1); /* vol control parameters= yes */
put_bits(&s->pb, 2, 1); /* chroma format YUV 420/YV12 */
put_bits(&s->pb, 1, s->low_delay);
put_bits(&s->pb, 1, 0); /* vbv parameters= no */
}
assert(MAX_LEVEL >= 64);
assert(MAX_RUN >= 63);
put_bits(&s->pb, 2, RECT_SHAPE); /* vol shape= rectangle */
put_bits(&s->pb, 1, 1); /* marker bit */
for(slevel=-64; slevel<64; slevel++){
if(slevel==0) continue;
for(run=0; run<64; run++){
for(last=0; last<=1; last++){
const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
int level= slevel < 0 ? -slevel : slevel;
int sign= slevel < 0 ? 1 : 0;
int bits, len, code;
put_bits(&s->pb, 16, s->avctx->time_base.den);
if (s->time_increment_bits < 1)
s->time_increment_bits = 1;
put_bits(&s->pb, 1, 1); /* marker bit */
put_bits(&s->pb, 1, 0); /* fixed vop rate=no */
put_bits(&s->pb, 1, 1); /* marker bit */
put_bits(&s->pb, 13, s->width); /* vol width */
put_bits(&s->pb, 1, 1); /* marker bit */
put_bits(&s->pb, 13, s->height); /* vol height */
put_bits(&s->pb, 1, 1); /* marker bit */
put_bits(&s->pb, 1, s->progressive_sequence ? 0 : 1);
put_bits(&s->pb, 1, 1); /* obmc disable */
if (vo_ver_id == 1) {
put_bits(&s->pb, 1, s->vol_sprite_usage); /* sprite enable */
}else{
put_bits(&s->pb, 2, s->vol_sprite_usage); /* sprite enable */
}
len_tab[index]= 100;
put_bits(&s->pb, 1, 0); /* not 8 bit == false */
put_bits(&s->pb, 1, s->mpeg_quant); /* quant type= (0=h263 style)*/
/* ESC0 */
code= get_rl_index(rl, last, run, level);
bits= rl->table_vlc[code][0];
len= rl->table_vlc[code][1];
bits=bits*2+sign; len++;
if(s->mpeg_quant){
ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
}
if(code!=rl->n && len < len_tab[index]){
if(bits_tab) bits_tab[index]= bits;
len_tab [index]= len;
}
/* ESC */
bits= rl->table_vlc[rl->n][0];
len = rl->table_vlc[rl->n][1];
bits=bits*2+last; len++;
bits=bits*64+run; len+=6;
bits=bits*256+(level&0xff); len+=8;
if (vo_ver_id != 1)
put_bits(&s->pb, 1, s->quarter_sample);
put_bits(&s->pb, 1, 1); /* complexity estimation disable */
s->resync_marker= s->rtp_mode;
put_bits(&s->pb, 1, s->resync_marker ? 0 : 1);/* resync marker disable */
put_bits(&s->pb, 1, s->data_partitioning ? 1 : 0);
if(s->data_partitioning){
put_bits(&s->pb, 1, 0); /* no rvlc */
}
if(len < len_tab[index]){
if(bits_tab) bits_tab[index]= bits;
len_tab [index]= len;
}
}
}
if (vo_ver_id != 1){
put_bits(&s->pb, 1, 0); /* newpred */
put_bits(&s->pb, 1, 0); /* reduced res vop */
}
put_bits(&s->pb, 1, 0); /* scalability */
ff_mpeg4_stuffing(&s->pb);
/* user data */
if(!(s->flags & CODEC_FLAG_BITEXACT)){
put_bits(&s->pb, 16, 0);
put_bits(&s->pb, 16, 0x1B2); /* user_data */
ff_put_string(&s->pb, LIBAVCODEC_IDENT, 0);
}
}
void h263_encode_init(MpegEncContext *s)
/* write mpeg4 VOP header */
void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
{
static int done = 0;
int time_incr;
int time_div, time_mod;
if (!done) {
done = 1;
if(s->pict_type==FF_I_TYPE){
if(!(s->flags&CODEC_FLAG_GLOBAL_HEADER)){
if(s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT) //HACK, the reference sw is buggy
mpeg4_encode_visual_object_header(s);
if(s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT || picture_number==0) //HACK, the reference sw is buggy
mpeg4_encode_vol_header(s, 0, 0);
}
if(!(s->workaround_bugs & FF_BUG_MS))
mpeg4_encode_gop_header(s);
}
init_uni_dc_tab();
s->partitioned_frame= s->data_partitioning && s->pict_type!=FF_B_TYPE;
init_rl(&rl_inter, static_rl_table_store[0]);
init_rl(&rl_intra, static_rl_table_store[1]);
init_rl(&rl_intra_aic, static_rl_table_store[2]);
put_bits(&s->pb, 16, 0); /* vop header */
put_bits(&s->pb, 16, VOP_STARTCODE); /* vop header */
put_bits(&s->pb, 2, s->pict_type - 1); /* pict type: I = 0 , P = 1 */
init_uni_mpeg4_rl_tab(&rl_intra, uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len);
init_uni_mpeg4_rl_tab(&rl_inter, uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len);
assert(s->time>=0);
time_div= s->time/s->avctx->time_base.den;
time_mod= s->time%s->avctx->time_base.den;
time_incr= time_div - s->last_time_base;
assert(time_incr >= 0);
while(time_incr--)
put_bits(&s->pb, 1, 1);
init_uni_h263_rl_tab(&rl_intra_aic, NULL, uni_h263_intra_aic_rl_len);
init_uni_h263_rl_tab(&rl_inter , NULL, uni_h263_inter_rl_len);
put_bits(&s->pb, 1, 0);
init_mv_penalty_and_fcode(s);
put_bits(&s->pb, 1, 1); /* marker */
put_bits(&s->pb, s->time_increment_bits, time_mod); /* time increment */
put_bits(&s->pb, 1, 1); /* marker */
put_bits(&s->pb, 1, 1); /* vop coded */
if ( s->pict_type == FF_P_TYPE
|| (s->pict_type == FF_S_TYPE && s->vol_sprite_usage==GMC_SPRITE)) {
put_bits(&s->pb, 1, s->no_rounding); /* rounding type */
}
s->me.mv_penalty= mv_penalty; //FIXME exact table for msmpeg4 & h263p
s->intra_ac_vlc_length =s->inter_ac_vlc_length = uni_h263_inter_rl_len;
s->intra_ac_vlc_last_length=s->inter_ac_vlc_last_length= uni_h263_inter_rl_len + 128*64;
if(s->h263_aic){
s->intra_ac_vlc_length = uni_h263_intra_aic_rl_len;
s->intra_ac_vlc_last_length= uni_h263_intra_aic_rl_len + 128*64;
put_bits(&s->pb, 3, 0); /* intra dc VLC threshold */
if(!s->progressive_sequence){
put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
put_bits(&s->pb, 1, s->alternate_scan);
}
s->ac_esc_length= 7+1+6+8;
//FIXME sprite stuff
// use fcodes >1 only for mpeg4 & h263 & h263p FIXME
switch(s->codec_id){
case CODEC_ID_MPEG4:
s->fcode_tab= fcode_tab;
s->min_qcoeff= -2048;
s->max_qcoeff= 2047;
s->intra_ac_vlc_length = uni_mpeg4_intra_rl_len;
s->intra_ac_vlc_last_length= uni_mpeg4_intra_rl_len + 128*64;
s->inter_ac_vlc_length = uni_mpeg4_inter_rl_len;
s->inter_ac_vlc_last_length= uni_mpeg4_inter_rl_len + 128*64;
s->luma_dc_vlc_length= uni_DCtab_lum_len;
s->chroma_dc_vlc_length= uni_DCtab_chrom_len;
s->ac_esc_length= 7+2+1+6+1+12+1;
s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
put_bits(&s->pb, 5, s->qscale);
if(s->flags & CODEC_FLAG_GLOBAL_HEADER){
if (s->pict_type != FF_I_TYPE)
put_bits(&s->pb, 3, s->f_code); /* fcode_for */
if (s->pict_type == FF_B_TYPE)
put_bits(&s->pb, 3, s->b_code); /* fcode_back */
}
s->avctx->extradata= av_malloc(1024);
init_put_bits(&s->pb, s->avctx->extradata, 1024);
void ff_h263_encode_motion(MpegEncContext * s, int val, int f_code)
{
int range, l, bit_size, sign, code, bits;
if(!(s->workaround_bugs & FF_BUG_MS))
mpeg4_encode_visual_object_header(s);
mpeg4_encode_vol_header(s, 0, 0);
if (val == 0) {
/* zero vector */
code = 0;
put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
} else {
bit_size = f_code - 1;
range = 1 << bit_size;
/* modulo encoding */
l= INT_BIT - 6 - bit_size;
val = (val<<l)>>l;
sign = val>>31;
val= (val^sign)-sign;
sign&=1;
// ff_mpeg4_stuffing(&s->pb); ?
flush_put_bits(&s->pb);
s->avctx->extradata_size= (put_bits_count(&s->pb)+7)>>3;
}
val--;
code = (val >> bit_size) + 1;
bits = val & (range - 1);
break;
case CODEC_ID_H263P:
if(s->umvplus)
s->fcode_tab= umv_fcode_tab;
if(s->modified_quant){
s->min_qcoeff= -2047;
s->max_qcoeff= 2047;
}else{
s->min_qcoeff= -127;
s->max_qcoeff= 127;
}
break;
//Note for mpeg4 & h263 the dc-scale table will be set per frame as needed later
case CODEC_ID_FLV1:
if (s->h263_flv > 1) {
s->min_qcoeff= -1023;
s->max_qcoeff= 1023;
} else {
s->min_qcoeff= -127;
s->max_qcoeff= 127;
put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
if (bit_size > 0) {
put_bits(&s->pb, bit_size, bits);
}
s->y_dc_scale_table=
s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
break;
default: //nothing needed - default table already set in mpegvideo.c
s->min_qcoeff= -127;
s->max_qcoeff= 127;
s->y_dc_scale_table=
s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
}
}
/***************************************************/
/**
* add mpeg4 stuffing bits (01...1)
*/
void ff_mpeg4_stuffing(PutBitContext * pbc)
static void init_mv_penalty_and_fcode(MpegEncContext *s)
{
int length;
put_bits(pbc, 1, 0);
length= (-put_bits_count(pbc))&7;
if(length) put_bits(pbc, length, (1<<length)-1);
}
/* must be called before writing the header */
void ff_set_mpeg4_time(MpegEncContext * s){
if(s->pict_type==FF_B_TYPE){
ff_mpeg4_init_direct_mv(s);
}else{
s->last_time_base= s->time_base;
s->time_base= s->time/s->avctx->time_base.den;
}
}
int f_code;
int mv;
static void mpeg4_encode_gop_header(MpegEncContext * s){
int hours, minutes, seconds;
int64_t time;
for(f_code=1; f_code<=MAX_FCODE; f_code++){
for(mv=-MAX_MV; mv<=MAX_MV; mv++){
int len;
put_bits(&s->pb, 16, 0);
put_bits(&s->pb, 16, GOP_STARTCODE);
if(mv==0) len= mvtab[0][1];
else{
int val, bit_size, code;
time= s->current_picture_ptr->pts;
if(s->reordered_input_picture[1])
time= FFMIN(time, s->reordered_input_picture[1]->pts);
time= time*s->avctx->time_base.num;
bit_size = f_code - 1;
seconds= time/s->avctx->time_base.den;
minutes= seconds/60; seconds %= 60;
hours= minutes/60; minutes %= 60;
hours%=24;
put_bits(&s->pb, 5, hours);
put_bits(&s->pb, 6, minutes);
put_bits(&s->pb, 1, 1);
put_bits(&s->pb, 6, seconds);
put_bits(&s->pb, 1, !!(s->flags&CODEC_FLAG_CLOSED_GOP));
put_bits(&s->pb, 1, 0); //broken link == NO
s->last_time_base= time / s->avctx->time_base.den;
ff_mpeg4_stuffing(&s->pb);
}
static void mpeg4_encode_visual_object_header(MpegEncContext * s){
int profile_and_level_indication;
int vo_ver_id;
val=mv;
if (val < 0)
val = -val;
val--;
code = (val >> bit_size) + 1;
if(code<33){
len= mvtab[code][1] + 1 + bit_size;
}else{
len= mvtab[32][1] + av_log2(code>>5) + 2 + bit_size;
}
}
if(s->avctx->profile != FF_PROFILE_UNKNOWN){
profile_and_level_indication = s->avctx->profile << 4;
}else if(s->max_b_frames || s->quarter_sample){
profile_and_level_indication= 0xF0; // adv simple
}else{
profile_and_level_indication= 0x00; // simple
mv_penalty[f_code][mv+MAX_MV]= len;
}
}
if(s->avctx->level != FF_LEVEL_UNKNOWN){
profile_and_level_indication |= s->avctx->level;
}else{
profile_and_level_indication |= 1; //level 1
for(f_code=MAX_FCODE; f_code>0; f_code--){
for(mv=-(16<<f_code); mv<(16<<f_code); mv++){
fcode_tab[mv+MAX_MV]= f_code;
}
}
if(profile_and_level_indication>>4 == 0xF){
vo_ver_id= 5;
}else{
vo_ver_id= 1;
for(mv=0; mv<MAX_MV*2+1; mv++){
umv_fcode_tab[mv]= 1;
}
}
//FIXME levels
static void init_uni_dc_tab(void)
{
int level, uni_code, uni_len;
put_bits(&s->pb, 16, 0);
put_bits(&s->pb, 16, VOS_STARTCODE);
for(level=-256; level<256; level++){
int size, v, l;
/* find number of bits */
size = 0;
v = abs(level);
while (v) {
v >>= 1;
size++;
}
put_bits(&s->pb, 8, profile_and_level_indication);
if (level < 0)
l= (-level) ^ ((1 << size) - 1);
else
l= level;
put_bits(&s->pb, 16, 0);
put_bits(&s->pb, 16, VISUAL_OBJ_STARTCODE);
/* luminance */
uni_code= DCtab_lum[size][0];
uni_len = DCtab_lum[size][1];
put_bits(&s->pb, 1, 1);
put_bits(&s->pb, 4, vo_ver_id);
put_bits(&s->pb, 3, 1); //priority
if (size > 0) {
uni_code<<=size; uni_code|=l;
uni_len+=size;
if (size > 8){
uni_code<<=1; uni_code|=1;
uni_len++;
}
}
uni_DCtab_lum_bits[level+256]= uni_code;
uni_DCtab_lum_len [level+256]= uni_len;
put_bits(&s->pb, 4, 1); //visual obj type== video obj
/* chrominance */
uni_code= DCtab_chrom[size][0];
uni_len = DCtab_chrom[size][1];
put_bits(&s->pb, 1, 0); //video signal type == no clue //FIXME
if (size > 0) {
uni_code<<=size; uni_code|=l;
uni_len+=size;
if (size > 8){
uni_code<<=1; uni_code|=1;
uni_len++;
}
}
uni_DCtab_chrom_bits[level+256]= uni_code;
uni_DCtab_chrom_len [level+256]= uni_len;
ff_mpeg4_stuffing(&s->pb);
}
}
static void mpeg4_encode_vol_header(MpegEncContext * s, int vo_number, int vol_number)
{
int vo_ver_id;
if (!CONFIG_MPEG4_ENCODER) return;
static void init_uni_mpeg4_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab){
int slevel, run, last;
if(s->max_b_frames || s->quarter_sample){
vo_ver_id= 5;
s->vo_type= ADV_SIMPLE_VO_TYPE;
}else{
vo_ver_id= 1;
s->vo_type= SIMPLE_VO_TYPE;
}
assert(MAX_LEVEL >= 64);
assert(MAX_RUN >= 63);
put_bits(&s->pb, 16, 0);
put_bits(&s->pb, 16, 0x100 + vo_number); /* video obj */
put_bits(&s->pb, 16, 0);
put_bits(&s->pb, 16, 0x120 + vol_number); /* video obj layer */
for(slevel=-64; slevel<64; slevel++){
if(slevel==0) continue;
for(run=0; run<64; run++){
for(last=0; last<=1; last++){
const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
int level= slevel < 0 ? -slevel : slevel;
int sign= slevel < 0 ? 1 : 0;
int bits, len, code;
int level1, run1;
put_bits(&s->pb, 1, 0); /* random access vol */
put_bits(&s->pb, 8, s->vo_type); /* video obj type indication */
if(s->workaround_bugs & FF_BUG_MS) {
put_bits(&s->pb, 1, 0); /* is obj layer id= no */
} else {
put_bits(&s->pb, 1, 1); /* is obj layer id= yes */
put_bits(&s->pb, 4, vo_ver_id); /* is obj layer ver id */
put_bits(&s->pb, 3, 1); /* is obj layer priority */
}
len_tab[index]= 100;
aspect_to_info(s, s->avctx->sample_aspect_ratio);
/* ESC0 */
code= get_rl_index(rl, last, run, level);
bits= rl->table_vlc[code][0];
len= rl->table_vlc[code][1];
bits=bits*2+sign; len++;
put_bits(&s->pb, 4, s->aspect_ratio_info);/* aspect ratio info */
if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){
put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
}
if(code!=rl->n && len < len_tab[index]){
bits_tab[index]= bits;
len_tab [index]= len;
}
/* ESC1 */
bits= rl->table_vlc[rl->n][0];
len= rl->table_vlc[rl->n][1];
bits=bits*2; len++; //esc1
level1= level - rl->max_level[last][run];
if(level1>0){
code= get_rl_index(rl, last, run, level1);
bits<<= rl->table_vlc[code][1];
len += rl->table_vlc[code][1];
bits += rl->table_vlc[code][0];
bits=bits*2+sign; len++;
if(s->workaround_bugs & FF_BUG_MS) { //
put_bits(&s->pb, 1, 0); /* vol control parameters= no @@@ */
} else {
put_bits(&s->pb, 1, 1); /* vol control parameters= yes */
put_bits(&s->pb, 2, 1); /* chroma format YUV 420/YV12 */
put_bits(&s->pb, 1, s->low_delay);
put_bits(&s->pb, 1, 0); /* vbv parameters= no */
}
if(code!=rl->n && len < len_tab[index]){
bits_tab[index]= bits;
len_tab [index]= len;
}
}
/* ESC2 */
bits= rl->table_vlc[rl->n][0];
len= rl->table_vlc[rl->n][1];
bits=bits*4+2; len+=2; //esc2
run1 = run - rl->max_run[last][level] - 1;
if(run1>=0){
code= get_rl_index(rl, last, run1, level);
bits<<= rl->table_vlc[code][1];
len += rl->table_vlc[code][1];
bits += rl->table_vlc[code][0];
bits=bits*2+sign; len++;
put_bits(&s->pb, 2, RECT_SHAPE); /* vol shape= rectangle */
put_bits(&s->pb, 1, 1); /* marker bit */
if(code!=rl->n && len < len_tab[index]){
bits_tab[index]= bits;
len_tab [index]= len;
}
}
/* ESC3 */
bits= rl->table_vlc[rl->n][0];
len = rl->table_vlc[rl->n][1];
bits=bits*4+3; len+=2; //esc3
bits=bits*2+last; len++;
bits=bits*64+run; len+=6;
bits=bits*2+1; len++; //marker
bits=bits*4096+(slevel&0xfff); len+=12;
bits=bits*2+1; len++; //marker
put_bits(&s->pb, 16, s->avctx->time_base.den);
if (s->time_increment_bits < 1)
s->time_increment_bits = 1;
put_bits(&s->pb, 1, 1); /* marker bit */
put_bits(&s->pb, 1, 0); /* fixed vop rate=no */
put_bits(&s->pb, 1, 1); /* marker bit */
put_bits(&s->pb, 13, s->width); /* vol width */
put_bits(&s->pb, 1, 1); /* marker bit */
put_bits(&s->pb, 13, s->height); /* vol height */
put_bits(&s->pb, 1, 1); /* marker bit */
put_bits(&s->pb, 1, s->progressive_sequence ? 0 : 1);
put_bits(&s->pb, 1, 1); /* obmc disable */
if (vo_ver_id == 1) {
put_bits(&s->pb, 1, s->vol_sprite_usage); /* sprite enable */
}else{
put_bits(&s->pb, 2, s->vol_sprite_usage); /* sprite enable */
if(len < len_tab[index]){
bits_tab[index]= bits;
len_tab [index]= len;
}
}
}
}
}
put_bits(&s->pb, 1, 0); /* not 8 bit == false */
put_bits(&s->pb, 1, s->mpeg_quant); /* quant type= (0=h263 style)*/
static void init_uni_h263_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab){
int slevel, run, last;
if(s->mpeg_quant){
ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
}
assert(MAX_LEVEL >= 64);
assert(MAX_RUN >= 63);
if (vo_ver_id != 1)
put_bits(&s->pb, 1, s->quarter_sample);
put_bits(&s->pb, 1, 1); /* complexity estimation disable */
s->resync_marker= s->rtp_mode;
put_bits(&s->pb, 1, s->resync_marker ? 0 : 1);/* resync marker disable */
put_bits(&s->pb, 1, s->data_partitioning ? 1 : 0);
if(s->data_partitioning){
put_bits(&s->pb, 1, 0); /* no rvlc */
}
for(slevel=-64; slevel<64; slevel++){
if(slevel==0) continue;
for(run=0; run<64; run++){
for(last=0; last<=1; last++){
const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
int level= slevel < 0 ? -slevel : slevel;
int sign= slevel < 0 ? 1 : 0;
int bits, len, code;
if (vo_ver_id != 1){
put_bits(&s->pb, 1, 0); /* newpred */
put_bits(&s->pb, 1, 0); /* reduced res vop */
}
put_bits(&s->pb, 1, 0); /* scalability */
len_tab[index]= 100;
ff_mpeg4_stuffing(&s->pb);
/* ESC0 */
code= get_rl_index(rl, last, run, level);
bits= rl->table_vlc[code][0];
len= rl->table_vlc[code][1];
bits=bits*2+sign; len++;
/* user data */
if(!(s->flags & CODEC_FLAG_BITEXACT)){
put_bits(&s->pb, 16, 0);
put_bits(&s->pb, 16, 0x1B2); /* user_data */
ff_put_string(&s->pb, LIBAVCODEC_IDENT, 0);
if(code!=rl->n && len < len_tab[index]){
if(bits_tab) bits_tab[index]= bits;
len_tab [index]= len;
}
/* ESC */
bits= rl->table_vlc[rl->n][0];
len = rl->table_vlc[rl->n][1];
bits=bits*2+last; len++;
bits=bits*64+run; len+=6;
bits=bits*256+(level&0xff); len+=8;
if(len < len_tab[index]){
if(bits_tab) bits_tab[index]= bits;
len_tab [index]= len;
}
}
}
}
}
/* write mpeg4 VOP header */
void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
void h263_encode_init(MpegEncContext *s)
{
int time_incr;
int time_div, time_mod;
static int done = 0;
if(s->pict_type==FF_I_TYPE){
if(!(s->flags&CODEC_FLAG_GLOBAL_HEADER)){
if(s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT) //HACK, the reference sw is buggy
mpeg4_encode_visual_object_header(s);
if(s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT || picture_number==0) //HACK, the reference sw is buggy
mpeg4_encode_vol_header(s, 0, 0);
}
if(!(s->workaround_bugs & FF_BUG_MS))
mpeg4_encode_gop_header(s);
}
if (!done) {
done = 1;
s->partitioned_frame= s->data_partitioning && s->pict_type!=FF_B_TYPE;
init_uni_dc_tab();
put_bits(&s->pb, 16, 0); /* vop header */
put_bits(&s->pb, 16, VOP_STARTCODE); /* vop header */
put_bits(&s->pb, 2, s->pict_type - 1); /* pict type: I = 0 , P = 1 */
init_rl(&rl_inter, static_rl_table_store[0]);
init_rl(&rl_intra, static_rl_table_store[1]);
init_rl(&rl_intra_aic, static_rl_table_store[2]);
assert(s->time>=0);
time_div= s->time/s->avctx->time_base.den;
time_mod= s->time%s->avctx->time_base.den;
time_incr= time_div - s->last_time_base;
assert(time_incr >= 0);
while(time_incr--)
put_bits(&s->pb, 1, 1);
init_uni_mpeg4_rl_tab(&rl_intra, uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len);
init_uni_mpeg4_rl_tab(&rl_inter, uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len);
put_bits(&s->pb, 1, 0);
init_uni_h263_rl_tab(&rl_intra_aic, NULL, uni_h263_intra_aic_rl_len);
init_uni_h263_rl_tab(&rl_inter , NULL, uni_h263_inter_rl_len);
put_bits(&s->pb, 1, 1); /* marker */
put_bits(&s->pb, s->time_increment_bits, time_mod); /* time increment */
put_bits(&s->pb, 1, 1); /* marker */
put_bits(&s->pb, 1, 1); /* vop coded */
if ( s->pict_type == FF_P_TYPE
|| (s->pict_type == FF_S_TYPE && s->vol_sprite_usage==GMC_SPRITE)) {
put_bits(&s->pb, 1, s->no_rounding); /* rounding type */
init_mv_penalty_and_fcode(s);
}
put_bits(&s->pb, 3, 0); /* intra dc VLC threshold */
if(!s->progressive_sequence){
put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
put_bits(&s->pb, 1, s->alternate_scan);
s->me.mv_penalty= mv_penalty; //FIXME exact table for msmpeg4 & h263p
s->intra_ac_vlc_length =s->inter_ac_vlc_length = uni_h263_inter_rl_len;
s->intra_ac_vlc_last_length=s->inter_ac_vlc_last_length= uni_h263_inter_rl_len + 128*64;
if(s->h263_aic){
s->intra_ac_vlc_length = uni_h263_intra_aic_rl_len;
s->intra_ac_vlc_last_length= uni_h263_intra_aic_rl_len + 128*64;
}
s->ac_esc_length= 7+1+6+8;
// use fcodes >1 only for mpeg4 & h263 & h263p FIXME
switch(s->codec_id){
case CODEC_ID_MPEG4:
s->fcode_tab= fcode_tab;
s->min_qcoeff= -2048;
s->max_qcoeff= 2047;
s->intra_ac_vlc_length = uni_mpeg4_intra_rl_len;
s->intra_ac_vlc_last_length= uni_mpeg4_intra_rl_len + 128*64;
s->inter_ac_vlc_length = uni_mpeg4_inter_rl_len;
s->inter_ac_vlc_last_length= uni_mpeg4_inter_rl_len + 128*64;
s->luma_dc_vlc_length= uni_DCtab_lum_len;
s->chroma_dc_vlc_length= uni_DCtab_chrom_len;
s->ac_esc_length= 7+2+1+6+1+12+1;
s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
if(s->flags & CODEC_FLAG_GLOBAL_HEADER){
s->avctx->extradata= av_malloc(1024);
init_put_bits(&s->pb, s->avctx->extradata, 1024);
if(!(s->workaround_bugs & FF_BUG_MS))
mpeg4_encode_visual_object_header(s);
mpeg4_encode_vol_header(s, 0, 0);
// ff_mpeg4_stuffing(&s->pb); ?
flush_put_bits(&s->pb);
s->avctx->extradata_size= (put_bits_count(&s->pb)+7)>>3;
}
break;
case CODEC_ID_H263P:
if(s->umvplus)
s->fcode_tab= umv_fcode_tab;
if(s->modified_quant){
s->min_qcoeff= -2047;
s->max_qcoeff= 2047;
}else{
s->min_qcoeff= -127;
s->max_qcoeff= 127;
}
break;
//Note for mpeg4 & h263 the dc-scale table will be set per frame as needed later
case CODEC_ID_FLV1:
if (s->h263_flv > 1) {
s->min_qcoeff= -1023;
s->max_qcoeff= 1023;
} else {
s->min_qcoeff= -127;
s->max_qcoeff= 127;
}
s->y_dc_scale_table=
s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
break;
default: //nothing needed - default table already set in mpegvideo.c
s->min_qcoeff= -127;
s->max_qcoeff= 127;
s->y_dc_scale_table=
s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
}
//FIXME sprite stuff
put_bits(&s->pb, 5, s->qscale);
if (s->pict_type != FF_I_TYPE)
put_bits(&s->pb, 3, s->f_code); /* fcode_for */
if (s->pict_type == FF_B_TYPE)
put_bits(&s->pb, 3, s->b_code); /* fcode_back */
}
#endif //CONFIG_ENCODERS
/**
......@@ -2606,234 +2819,6 @@ void mpeg4_pred_ac(MpegEncContext * s, DCTELEM *block, int n,
}
#if CONFIG_ENCODERS
/**
* encodes the dc value.
* @param n block index (0-3 are luma, 4-5 are chroma)
*/
static inline void mpeg4_encode_dc(PutBitContext * s, int level, int n)
{
#if 1
level+=256;
if (n < 4) {
/* luminance */
put_bits(s, uni_DCtab_lum_len[level], uni_DCtab_lum_bits[level]);
} else {
/* chrominance */
put_bits(s, uni_DCtab_chrom_len[level], uni_DCtab_chrom_bits[level]);
}
#else
int size, v;
/* find number of bits */
size = 0;
v = abs(level);
while (v) {
v >>= 1;
size++;
}
if (n < 4) {
/* luminance */
put_bits(&s->pb, DCtab_lum[size][1], DCtab_lum[size][0]);
} else {
/* chrominance */
put_bits(&s->pb, DCtab_chrom[size][1], DCtab_chrom[size][0]);
}
/* encode remaining bits */
if (size > 0) {
if (level < 0)
level = (-level) ^ ((1 << size) - 1);
put_bits(&s->pb, size, level);
if (size > 8)
put_bits(&s->pb, 1, 1);
}
#endif
}
static inline int mpeg4_get_dc_length(int level, int n){
if (n < 4) {
return uni_DCtab_lum_len[level + 256];
} else {
return uni_DCtab_chrom_len[level + 256];
}
}
/**
* encodes a 8x8 block
* @param n block index (0-3 are luma, 4-5 are chroma)
*/
static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n, int intra_dc,
uint8_t *scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb)
{
int i, last_non_zero;
#if 0 //variables for the outcommented version
int code, sign, last;
#endif
const RLTable *rl;
uint32_t *bits_tab;
uint8_t *len_tab;
const int last_index = s->block_last_index[n];
if (s->mb_intra) { //Note gcc (3.2.1 at least) will optimize this away
/* mpeg4 based DC predictor */
mpeg4_encode_dc(dc_pb, intra_dc, n);
if(last_index<1) return;
i = 1;
rl = &rl_intra;
bits_tab= uni_mpeg4_intra_rl_bits;
len_tab = uni_mpeg4_intra_rl_len;
} else {
if(last_index<0) return;
i = 0;
rl = &rl_inter;
bits_tab= uni_mpeg4_inter_rl_bits;
len_tab = uni_mpeg4_inter_rl_len;
}
/* AC coefs */
last_non_zero = i - 1;
#if 1
for (; i < last_index; i++) {
int level = block[ scan_table[i] ];
if (level) {
int run = i - last_non_zero - 1;
level+=64;
if((level&(~127)) == 0){
const int index= UNI_MPEG4_ENC_INDEX(0, run, level);
put_bits(ac_pb, len_tab[index], bits_tab[index]);
}else{ //ESC3
put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(0<<20)+(run<<14)+(1<<13)+(((level-64)&0xfff)<<1)+1);
}
last_non_zero = i;
}
}
/*if(i<=last_index)*/{
int level = block[ scan_table[i] ];
int run = i - last_non_zero - 1;
level+=64;
if((level&(~127)) == 0){
const int index= UNI_MPEG4_ENC_INDEX(1, run, level);
put_bits(ac_pb, len_tab[index], bits_tab[index]);
}else{ //ESC3
put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(1<<20)+(run<<14)+(1<<13)+(((level-64)&0xfff)<<1)+1);
}
}
#else
for (; i <= last_index; i++) {
const int slevel = block[ scan_table[i] ];
if (slevel) {
int level;
int run = i - last_non_zero - 1;
last = (i == last_index);
sign = 0;
level = slevel;
if (level < 0) {
sign = 1;
level = -level;
}
code = get_rl_index(rl, last, run, level);
put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
if (code == rl->n) {
int level1, run1;
level1 = level - rl->max_level[last][run];
if (level1 < 1)
goto esc2;
code = get_rl_index(rl, last, run, level1);
if (code == rl->n) {
esc2:
put_bits(ac_pb, 1, 1);
if (level > MAX_LEVEL)
goto esc3;
run1 = run - rl->max_run[last][level] - 1;
if (run1 < 0)
goto esc3;
code = get_rl_index(rl, last, run1, level);
if (code == rl->n) {
esc3:
/* third escape */
put_bits(ac_pb, 1, 1);
put_bits(ac_pb, 1, last);
put_bits(ac_pb, 6, run);
put_bits(ac_pb, 1, 1);
put_sbits(ac_pb, 12, slevel);
put_bits(ac_pb, 1, 1);
} else {
/* second escape */
put_bits(ac_pb, 1, 0);
put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
put_bits(ac_pb, 1, sign);
}
} else {
/* first escape */
put_bits(ac_pb, 1, 0);
put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
put_bits(ac_pb, 1, sign);
}
} else {
put_bits(ac_pb, 1, sign);
}
last_non_zero = i;
}
}
#endif
}
static int mpeg4_get_block_length(MpegEncContext * s, DCTELEM * block, int n, int intra_dc,
uint8_t *scan_table)
{
int i, last_non_zero;
uint8_t *len_tab;
const int last_index = s->block_last_index[n];
int len=0;
if (s->mb_intra) { //Note gcc (3.2.1 at least) will optimize this away
/* mpeg4 based DC predictor */
len += mpeg4_get_dc_length(intra_dc, n);
if(last_index<1) return len;
i = 1;
len_tab = uni_mpeg4_intra_rl_len;
} else {
if(last_index<0) return 0;
i = 0;
len_tab = uni_mpeg4_inter_rl_len;
}
/* AC coefs */
last_non_zero = i - 1;
for (; i < last_index; i++) {
int level = block[ scan_table[i] ];
if (level) {
int run = i - last_non_zero - 1;
level+=64;
if((level&(~127)) == 0){
const int index= UNI_MPEG4_ENC_INDEX(0, run, level);
len += len_tab[index];
}else{ //ESC3
len += 7+2+1+6+1+12+1;
}
last_non_zero = i;
}
}
/*if(i<=last_index)*/{
int level = block[ scan_table[i] ];
int run = i - last_non_zero - 1;
level+=64;
if((level&(~127)) == 0){
const int index= UNI_MPEG4_ENC_INDEX(1, run, level);
len += len_tab[index];
}else{ //ESC3
len += 7+2+1+6+1+12+1;
}
}
return len;
}
#endif
/***********************************************/
/* decoding */
......
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