Revision 20390e31

View differences:

libavcodec/h263.c
57 57
#define CBPC_B_VLC_BITS 3
58 58

  
59 59
#if CONFIG_ENCODERS
60
static void mpeg4_encode_visual_object_header(MpegEncContext *s);
61
static void mpeg4_encode_vol_header(MpegEncContext *s, int vo_number,
62
                                    int vol_number);
63
static inline void mpeg4_encode_block(MpegEncContext *s, DCTELEM *block,
64
                                      int n, int dc, uint8_t *scan_table,
65
                                      PutBitContext *dc_pb,
66
                                      PutBitContext *ac_pb);
67
static int mpeg4_get_block_length(MpegEncContext *s, DCTELEM *block, int n,
68
                                  int intra_dc, uint8_t *scan_table);
69

  
70 60
static uint8_t uni_DCtab_lum_len[512];
71 61
static uint8_t uni_DCtab_chrom_len[512];
72 62
static uint16_t uni_DCtab_lum_bits[512];
......
788 778

  
789 779
#if CONFIG_ENCODERS
790 780

  
781
/**
782
 * encodes the dc value.
783
 * @param n block index (0-3 are luma, 4-5 are chroma)
784
 */
785
static inline void mpeg4_encode_dc(PutBitContext * s, int level, int n)
786
{
787
#if 1
788
    level+=256;
789
    if (n < 4) {
790
        /* luminance */
791
        put_bits(s, uni_DCtab_lum_len[level], uni_DCtab_lum_bits[level]);
792
    } else {
793
        /* chrominance */
794
        put_bits(s, uni_DCtab_chrom_len[level], uni_DCtab_chrom_bits[level]);
795
    }
796
#else
797
    int size, v;
798
    /* find number of bits */
799
    size = 0;
800
    v = abs(level);
801
    while (v) {
802
        v >>= 1;
803
        size++;
804
    }
805

  
806
    if (n < 4) {
807
        /* luminance */
808
        put_bits(&s->pb, DCtab_lum[size][1], DCtab_lum[size][0]);
809
    } else {
810
        /* chrominance */
811
        put_bits(&s->pb, DCtab_chrom[size][1], DCtab_chrom[size][0]);
812
    }
813

  
814
    /* encode remaining bits */
815
    if (size > 0) {
816
        if (level < 0)
817
            level = (-level) ^ ((1 << size) - 1);
818
        put_bits(&s->pb, size, level);
819
        if (size > 8)
820
            put_bits(&s->pb, 1, 1);
821
    }
822
#endif
823
}
824

  
825
static inline int mpeg4_get_dc_length(int level, int n){
826
    if (n < 4) {
827
        return uni_DCtab_lum_len[level + 256];
828
    } else {
829
        return uni_DCtab_chrom_len[level + 256];
830
    }
831
}
832

  
833
/**
834
 * encodes a 8x8 block
835
 * @param n block index (0-3 are luma, 4-5 are chroma)
836
 */
837
static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n, int intra_dc,
838
                               uint8_t *scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb)
839
{
840
    int i, last_non_zero;
841
#if 0 //variables for the outcommented version
842
    int code, sign, last;
843
#endif
844
    const RLTable *rl;
845
    uint32_t *bits_tab;
846
    uint8_t *len_tab;
847
    const int last_index = s->block_last_index[n];
848

  
849
    if (s->mb_intra) { //Note gcc (3.2.1 at least) will optimize this away
850
        /* mpeg4 based DC predictor */
851
        mpeg4_encode_dc(dc_pb, intra_dc, n);
852
        if(last_index<1) return;
853
        i = 1;
854
        rl = &rl_intra;
855
        bits_tab= uni_mpeg4_intra_rl_bits;
856
        len_tab = uni_mpeg4_intra_rl_len;
857
    } else {
858
        if(last_index<0) return;
859
        i = 0;
860
        rl = &rl_inter;
861
        bits_tab= uni_mpeg4_inter_rl_bits;
862
        len_tab = uni_mpeg4_inter_rl_len;
863
    }
864

  
865
    /* AC coefs */
866
    last_non_zero = i - 1;
867
#if 1
868
    for (; i < last_index; i++) {
869
        int level = block[ scan_table[i] ];
870
        if (level) {
871
            int run = i - last_non_zero - 1;
872
            level+=64;
873
            if((level&(~127)) == 0){
874
                const int index= UNI_MPEG4_ENC_INDEX(0, run, level);
875
                put_bits(ac_pb, len_tab[index], bits_tab[index]);
876
            }else{ //ESC3
877
                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);
878
            }
879
            last_non_zero = i;
880
        }
881
    }
882
    /*if(i<=last_index)*/{
883
        int level = block[ scan_table[i] ];
884
        int run = i - last_non_zero - 1;
885
        level+=64;
886
        if((level&(~127)) == 0){
887
            const int index= UNI_MPEG4_ENC_INDEX(1, run, level);
888
            put_bits(ac_pb, len_tab[index], bits_tab[index]);
889
        }else{ //ESC3
890
            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);
891
        }
892
    }
893
#else
894
    for (; i <= last_index; i++) {
895
        const int slevel = block[ scan_table[i] ];
896
        if (slevel) {
897
            int level;
898
            int run = i - last_non_zero - 1;
899
            last = (i == last_index);
900
            sign = 0;
901
            level = slevel;
902
            if (level < 0) {
903
                sign = 1;
904
                level = -level;
905
            }
906
            code = get_rl_index(rl, last, run, level);
907
            put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
908
            if (code == rl->n) {
909
                int level1, run1;
910
                level1 = level - rl->max_level[last][run];
911
                if (level1 < 1)
912
                    goto esc2;
913
                code = get_rl_index(rl, last, run, level1);
914
                if (code == rl->n) {
915
                esc2:
916
                    put_bits(ac_pb, 1, 1);
917
                    if (level > MAX_LEVEL)
918
                        goto esc3;
919
                    run1 = run - rl->max_run[last][level] - 1;
920
                    if (run1 < 0)
921
                        goto esc3;
922
                    code = get_rl_index(rl, last, run1, level);
923
                    if (code == rl->n) {
924
                    esc3:
925
                        /* third escape */
926
                        put_bits(ac_pb, 1, 1);
927
                        put_bits(ac_pb, 1, last);
928
                        put_bits(ac_pb, 6, run);
929
                        put_bits(ac_pb, 1, 1);
930
                        put_sbits(ac_pb, 12, slevel);
931
                        put_bits(ac_pb, 1, 1);
932
                    } else {
933
                        /* second escape */
934
                        put_bits(ac_pb, 1, 0);
935
                        put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
936
                        put_bits(ac_pb, 1, sign);
937
                    }
938
                } else {
939
                    /* first escape */
940
                    put_bits(ac_pb, 1, 0);
941
                    put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
942
                    put_bits(ac_pb, 1, sign);
943
                }
944
            } else {
945
                put_bits(ac_pb, 1, sign);
946
            }
947
            last_non_zero = i;
948
        }
949
    }
950
#endif
951
}
952

  
953
static int mpeg4_get_block_length(MpegEncContext * s, DCTELEM * block, int n, int intra_dc,
954
                               uint8_t *scan_table)
955
{
956
    int i, last_non_zero;
957
    uint8_t *len_tab;
958
    const int last_index = s->block_last_index[n];
959
    int len=0;
960

  
961
    if (s->mb_intra) { //Note gcc (3.2.1 at least) will optimize this away
962
        /* mpeg4 based DC predictor */
963
        len += mpeg4_get_dc_length(intra_dc, n);
964
        if(last_index<1) return len;
965
        i = 1;
966
        len_tab = uni_mpeg4_intra_rl_len;
967
    } else {
968
        if(last_index<0) return 0;
969
        i = 0;
970
        len_tab = uni_mpeg4_inter_rl_len;
971
    }
972

  
973
    /* AC coefs */
974
    last_non_zero = i - 1;
975
    for (; i < last_index; i++) {
976
        int level = block[ scan_table[i] ];
977
        if (level) {
978
            int run = i - last_non_zero - 1;
979
            level+=64;
980
            if((level&(~127)) == 0){
981
                const int index= UNI_MPEG4_ENC_INDEX(0, run, level);
982
                len += len_tab[index];
983
            }else{ //ESC3
984
                len += 7+2+1+6+1+12+1;
985
            }
986
            last_non_zero = i;
987
        }
988
    }
989
    /*if(i<=last_index)*/{
990
        int level = block[ scan_table[i] ];
991
        int run = i - last_non_zero - 1;
992
        level+=64;
993
        if((level&(~127)) == 0){
994
            const int index= UNI_MPEG4_ENC_INDEX(1, run, level);
995
            len += len_tab[index];
996
        }else{ //ESC3
997
            len += 7+2+1+6+1+12+1;
998
        }
999
    }
1000

  
1001
    return len;
1002
}
1003

  
791 1004
static inline int h263_get_motion_length(MpegEncContext * s, int val, int f_code){
792 1005
    int l, bit_size, code;
793 1006

  
......
1950 2163
}
1951 2164

  
1952 2165
#if CONFIG_ENCODERS
1953
void ff_h263_encode_motion(MpegEncContext * s, int val, int f_code)
1954
{
1955
    int range, l, bit_size, sign, code, bits;
1956

  
1957
    if (val == 0) {
1958
        /* zero vector */
1959
        code = 0;
1960
        put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
1961
    } else {
1962
        bit_size = f_code - 1;
1963
        range = 1 << bit_size;
1964
        /* modulo encoding */
1965
        l= INT_BIT - 6 - bit_size;
1966
        val = (val<<l)>>l;
1967
        sign = val>>31;
1968
        val= (val^sign)-sign;
1969
        sign&=1;
1970 2166

  
1971
        val--;
1972
        code = (val >> bit_size) + 1;
1973
        bits = val & (range - 1);
2167
/***************************************************/
2168
/**
2169
 * add mpeg4 stuffing bits (01...1)
2170
 */
2171
void ff_mpeg4_stuffing(PutBitContext * pbc)
2172
{
2173
    int length;
2174
    put_bits(pbc, 1, 0);
2175
    length= (-put_bits_count(pbc))&7;
2176
    if(length) put_bits(pbc, length, (1<<length)-1);
2177
}
1974 2178

  
1975
        put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
1976
        if (bit_size > 0) {
1977
            put_bits(&s->pb, bit_size, bits);
1978
        }
2179
/* must be called before writing the header */
2180
void ff_set_mpeg4_time(MpegEncContext * s){
2181
    if(s->pict_type==FF_B_TYPE){
2182
        ff_mpeg4_init_direct_mv(s);
2183
    }else{
2184
        s->last_time_base= s->time_base;
2185
        s->time_base= s->time/s->avctx->time_base.den;
1979 2186
    }
1980 2187
}
1981 2188

  
1982
static void init_mv_penalty_and_fcode(MpegEncContext *s)
1983
{
1984
    int f_code;
1985
    int mv;
2189
static void mpeg4_encode_gop_header(MpegEncContext * s){
2190
    int hours, minutes, seconds;
2191
    int64_t time;
1986 2192

  
1987
    for(f_code=1; f_code<=MAX_FCODE; f_code++){
1988
        for(mv=-MAX_MV; mv<=MAX_MV; mv++){
1989
            int len;
2193
    put_bits(&s->pb, 16, 0);
2194
    put_bits(&s->pb, 16, GOP_STARTCODE);
1990 2195

  
1991
            if(mv==0) len= mvtab[0][1];
1992
            else{
1993
                int val, bit_size, code;
2196
    time= s->current_picture_ptr->pts;
2197
    if(s->reordered_input_picture[1])
2198
        time= FFMIN(time, s->reordered_input_picture[1]->pts);
2199
    time= time*s->avctx->time_base.num;
1994 2200

  
1995
                bit_size = f_code - 1;
2201
    seconds= time/s->avctx->time_base.den;
2202
    minutes= seconds/60; seconds %= 60;
2203
    hours= minutes/60; minutes %= 60;
2204
    hours%=24;
1996 2205

  
1997
                val=mv;
1998
                if (val < 0)
1999
                    val = -val;
2000
                val--;
2001
                code = (val >> bit_size) + 1;
2002
                if(code<33){
2003
                    len= mvtab[code][1] + 1 + bit_size;
2004
                }else{
2005
                    len= mvtab[32][1] + av_log2(code>>5) + 2 + bit_size;
2006
                }
2007
            }
2206
    put_bits(&s->pb, 5, hours);
2207
    put_bits(&s->pb, 6, minutes);
2208
    put_bits(&s->pb, 1, 1);
2209
    put_bits(&s->pb, 6, seconds);
2008 2210

  
2009
            mv_penalty[f_code][mv+MAX_MV]= len;
2010
        }
2211
    put_bits(&s->pb, 1, !!(s->flags&CODEC_FLAG_CLOSED_GOP));
2212
    put_bits(&s->pb, 1, 0); //broken link == NO
2213

  
2214
    s->last_time_base= time / s->avctx->time_base.den;
2215

  
2216
    ff_mpeg4_stuffing(&s->pb);
2217
}
2218

  
2219
static void mpeg4_encode_visual_object_header(MpegEncContext * s){
2220
    int profile_and_level_indication;
2221
    int vo_ver_id;
2222

  
2223
    if(s->avctx->profile != FF_PROFILE_UNKNOWN){
2224
        profile_and_level_indication = s->avctx->profile << 4;
2225
    }else if(s->max_b_frames || s->quarter_sample){
2226
        profile_and_level_indication= 0xF0; // adv simple
2227
    }else{
2228
        profile_and_level_indication= 0x00; // simple
2011 2229
    }
2012 2230

  
2013
    for(f_code=MAX_FCODE; f_code>0; f_code--){
2014
        for(mv=-(16<<f_code); mv<(16<<f_code); mv++){
2015
            fcode_tab[mv+MAX_MV]= f_code;
2016
        }
2231
    if(s->avctx->level != FF_LEVEL_UNKNOWN){
2232
        profile_and_level_indication |= s->avctx->level;
2233
    }else{
2234
        profile_and_level_indication |= 1; //level 1
2017 2235
    }
2018 2236

  
2019
    for(mv=0; mv<MAX_MV*2+1; mv++){
2020
        umv_fcode_tab[mv]= 1;
2237
    if(profile_and_level_indication>>4 == 0xF){
2238
        vo_ver_id= 5;
2239
    }else{
2240
        vo_ver_id= 1;
2021 2241
    }
2022
}
2023 2242

  
2024
static void init_uni_dc_tab(void)
2025
{
2026
    int level, uni_code, uni_len;
2243
    //FIXME levels
2027 2244

  
2028
    for(level=-256; level<256; level++){
2029
        int size, v, l;
2030
        /* find number of bits */
2031
        size = 0;
2032
        v = abs(level);
2033
        while (v) {
2034
            v >>= 1;
2035
            size++;
2036
        }
2245
    put_bits(&s->pb, 16, 0);
2246
    put_bits(&s->pb, 16, VOS_STARTCODE);
2037 2247

  
2038
        if (level < 0)
2039
            l= (-level) ^ ((1 << size) - 1);
2040
        else
2041
            l= level;
2248
    put_bits(&s->pb, 8, profile_and_level_indication);
2042 2249

  
2043
        /* luminance */
2044
        uni_code= DCtab_lum[size][0];
2045
        uni_len = DCtab_lum[size][1];
2250
    put_bits(&s->pb, 16, 0);
2251
    put_bits(&s->pb, 16, VISUAL_OBJ_STARTCODE);
2046 2252

  
2047
        if (size > 0) {
2048
            uni_code<<=size; uni_code|=l;
2049
            uni_len+=size;
2050
            if (size > 8){
2051
                uni_code<<=1; uni_code|=1;
2052
                uni_len++;
2053
            }
2054
        }
2055
        uni_DCtab_lum_bits[level+256]= uni_code;
2056
        uni_DCtab_lum_len [level+256]= uni_len;
2253
    put_bits(&s->pb, 1, 1);
2254
        put_bits(&s->pb, 4, vo_ver_id);
2255
        put_bits(&s->pb, 3, 1); //priority
2057 2256

  
2058
        /* chrominance */
2059
        uni_code= DCtab_chrom[size][0];
2060
        uni_len = DCtab_chrom[size][1];
2257
    put_bits(&s->pb, 4, 1); //visual obj type== video obj
2061 2258

  
2062
        if (size > 0) {
2063
            uni_code<<=size; uni_code|=l;
2064
            uni_len+=size;
2065
            if (size > 8){
2066
                uni_code<<=1; uni_code|=1;
2067
                uni_len++;
2068
            }
2069
        }
2070
        uni_DCtab_chrom_bits[level+256]= uni_code;
2071
        uni_DCtab_chrom_len [level+256]= uni_len;
2259
    put_bits(&s->pb, 1, 0); //video signal type == no clue //FIXME
2072 2260

  
2073
    }
2261
    ff_mpeg4_stuffing(&s->pb);
2074 2262
}
2075 2263

  
2076
static void init_uni_mpeg4_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab){
2077
    int slevel, run, last;
2078

  
2079
    assert(MAX_LEVEL >= 64);
2080
    assert(MAX_RUN   >= 63);
2081

  
2082
    for(slevel=-64; slevel<64; slevel++){
2083
        if(slevel==0) continue;
2084
        for(run=0; run<64; run++){
2085
            for(last=0; last<=1; last++){
2086
                const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
2087
                int level= slevel < 0 ? -slevel : slevel;
2088
                int sign= slevel < 0 ? 1 : 0;
2089
                int bits, len, code;
2090
                int level1, run1;
2264
static void mpeg4_encode_vol_header(MpegEncContext * s, int vo_number, int vol_number)
2265
{
2266
    int vo_ver_id;
2091 2267

  
2092
                len_tab[index]= 100;
2268
    if (!CONFIG_MPEG4_ENCODER)  return;
2093 2269

  
2094
                /* ESC0 */
2095
                code= get_rl_index(rl, last, run, level);
2096
                bits= rl->table_vlc[code][0];
2097
                len=  rl->table_vlc[code][1];
2098
                bits=bits*2+sign; len++;
2270
    if(s->max_b_frames || s->quarter_sample){
2271
        vo_ver_id= 5;
2272
        s->vo_type= ADV_SIMPLE_VO_TYPE;
2273
    }else{
2274
        vo_ver_id= 1;
2275
        s->vo_type= SIMPLE_VO_TYPE;
2276
    }
2099 2277

  
2100
                if(code!=rl->n && len < len_tab[index]){
2101
                    bits_tab[index]= bits;
2102
                    len_tab [index]= len;
2103
                }
2104
                /* ESC1 */
2105
                bits= rl->table_vlc[rl->n][0];
2106
                len=  rl->table_vlc[rl->n][1];
2107
                bits=bits*2;    len++; //esc1
2108
                level1= level - rl->max_level[last][run];
2109
                if(level1>0){
2110
                    code= get_rl_index(rl, last, run, level1);
2111
                    bits<<= rl->table_vlc[code][1];
2112
                    len  += rl->table_vlc[code][1];
2113
                    bits += rl->table_vlc[code][0];
2114
                    bits=bits*2+sign; len++;
2278
    put_bits(&s->pb, 16, 0);
2279
    put_bits(&s->pb, 16, 0x100 + vo_number);        /* video obj */
2280
    put_bits(&s->pb, 16, 0);
2281
    put_bits(&s->pb, 16, 0x120 + vol_number);       /* video obj layer */
2115 2282

  
2116
                    if(code!=rl->n && len < len_tab[index]){
2117
                        bits_tab[index]= bits;
2118
                        len_tab [index]= len;
2119
                    }
2120
                }
2121
                /* ESC2 */
2122
                bits= rl->table_vlc[rl->n][0];
2123
                len=  rl->table_vlc[rl->n][1];
2124
                bits=bits*4+2;    len+=2; //esc2
2125
                run1 = run - rl->max_run[last][level] - 1;
2126
                if(run1>=0){
2127
                    code= get_rl_index(rl, last, run1, level);
2128
                    bits<<= rl->table_vlc[code][1];
2129
                    len  += rl->table_vlc[code][1];
2130
                    bits += rl->table_vlc[code][0];
2131
                    bits=bits*2+sign; len++;
2283
    put_bits(&s->pb, 1, 0);             /* random access vol */
2284
    put_bits(&s->pb, 8, s->vo_type);    /* video obj type indication */
2285
    if(s->workaround_bugs & FF_BUG_MS) {
2286
        put_bits(&s->pb, 1, 0);         /* is obj layer id= no */
2287
    } else {
2288
        put_bits(&s->pb, 1, 1);         /* is obj layer id= yes */
2289
        put_bits(&s->pb, 4, vo_ver_id); /* is obj layer ver id */
2290
        put_bits(&s->pb, 3, 1);         /* is obj layer priority */
2291
    }
2132 2292

  
2133
                    if(code!=rl->n && len < len_tab[index]){
2134
                        bits_tab[index]= bits;
2135
                        len_tab [index]= len;
2136
                    }
2137
                }
2138
                /* ESC3 */
2139
                bits= rl->table_vlc[rl->n][0];
2140
                len = rl->table_vlc[rl->n][1];
2141
                bits=bits*4+3;    len+=2; //esc3
2142
                bits=bits*2+last; len++;
2143
                bits=bits*64+run; len+=6;
2144
                bits=bits*2+1;    len++;  //marker
2145
                bits=bits*4096+(slevel&0xfff); len+=12;
2146
                bits=bits*2+1;    len++;  //marker
2293
    aspect_to_info(s, s->avctx->sample_aspect_ratio);
2147 2294

  
2148
                if(len < len_tab[index]){
2149
                    bits_tab[index]= bits;
2150
                    len_tab [index]= len;
2151
                }
2152
            }
2153
        }
2295
    put_bits(&s->pb, 4, s->aspect_ratio_info);/* aspect ratio info */
2296
    if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){
2297
        put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
2298
        put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
2154 2299
    }
2155
}
2156 2300

  
2157
static void init_uni_h263_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab){
2158
    int slevel, run, last;
2301
    if(s->workaround_bugs & FF_BUG_MS) { //
2302
        put_bits(&s->pb, 1, 0);         /* vol control parameters= no @@@ */
2303
    } else {
2304
        put_bits(&s->pb, 1, 1);         /* vol control parameters= yes */
2305
        put_bits(&s->pb, 2, 1);         /* chroma format YUV 420/YV12 */
2306
        put_bits(&s->pb, 1, s->low_delay);
2307
        put_bits(&s->pb, 1, 0);         /* vbv parameters= no */
2308
    }
2159 2309

  
2160
    assert(MAX_LEVEL >= 64);
2161
    assert(MAX_RUN   >= 63);
2310
    put_bits(&s->pb, 2, RECT_SHAPE);    /* vol shape= rectangle */
2311
    put_bits(&s->pb, 1, 1);             /* marker bit */
2162 2312

  
2163
    for(slevel=-64; slevel<64; slevel++){
2164
        if(slevel==0) continue;
2165
        for(run=0; run<64; run++){
2166
            for(last=0; last<=1; last++){
2167
                const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
2168
                int level= slevel < 0 ? -slevel : slevel;
2169
                int sign= slevel < 0 ? 1 : 0;
2170
                int bits, len, code;
2313
    put_bits(&s->pb, 16, s->avctx->time_base.den);
2314
    if (s->time_increment_bits < 1)
2315
        s->time_increment_bits = 1;
2316
    put_bits(&s->pb, 1, 1);             /* marker bit */
2317
    put_bits(&s->pb, 1, 0);             /* fixed vop rate=no */
2318
    put_bits(&s->pb, 1, 1);             /* marker bit */
2319
    put_bits(&s->pb, 13, s->width);     /* vol width */
2320
    put_bits(&s->pb, 1, 1);             /* marker bit */
2321
    put_bits(&s->pb, 13, s->height);    /* vol height */
2322
    put_bits(&s->pb, 1, 1);             /* marker bit */
2323
    put_bits(&s->pb, 1, s->progressive_sequence ? 0 : 1);
2324
    put_bits(&s->pb, 1, 1);             /* obmc disable */
2325
    if (vo_ver_id == 1) {
2326
        put_bits(&s->pb, 1, s->vol_sprite_usage);       /* sprite enable */
2327
    }else{
2328
        put_bits(&s->pb, 2, s->vol_sprite_usage);       /* sprite enable */
2329
    }
2171 2330

  
2172
                len_tab[index]= 100;
2331
    put_bits(&s->pb, 1, 0);             /* not 8 bit == false */
2332
    put_bits(&s->pb, 1, s->mpeg_quant); /* quant type= (0=h263 style)*/
2173 2333

  
2174
                /* ESC0 */
2175
                code= get_rl_index(rl, last, run, level);
2176
                bits= rl->table_vlc[code][0];
2177
                len=  rl->table_vlc[code][1];
2178
                bits=bits*2+sign; len++;
2334
    if(s->mpeg_quant){
2335
        ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
2336
        ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
2337
    }
2179 2338

  
2180
                if(code!=rl->n && len < len_tab[index]){
2181
                    if(bits_tab) bits_tab[index]= bits;
2182
                    len_tab [index]= len;
2183
                }
2184
                /* ESC */
2185
                bits= rl->table_vlc[rl->n][0];
2186
                len = rl->table_vlc[rl->n][1];
2187
                bits=bits*2+last; len++;
2188
                bits=bits*64+run; len+=6;
2189
                bits=bits*256+(level&0xff); len+=8;
2339
    if (vo_ver_id != 1)
2340
        put_bits(&s->pb, 1, s->quarter_sample);
2341
    put_bits(&s->pb, 1, 1);             /* complexity estimation disable */
2342
    s->resync_marker= s->rtp_mode;
2343
    put_bits(&s->pb, 1, s->resync_marker ? 0 : 1);/* resync marker disable */
2344
    put_bits(&s->pb, 1, s->data_partitioning ? 1 : 0);
2345
    if(s->data_partitioning){
2346
        put_bits(&s->pb, 1, 0);         /* no rvlc */
2347
    }
2190 2348

  
2191
                if(len < len_tab[index]){
2192
                    if(bits_tab) bits_tab[index]= bits;
2193
                    len_tab [index]= len;
2194
                }
2195
            }
2196
        }
2349
    if (vo_ver_id != 1){
2350
        put_bits(&s->pb, 1, 0);         /* newpred */
2351
        put_bits(&s->pb, 1, 0);         /* reduced res vop */
2352
    }
2353
    put_bits(&s->pb, 1, 0);             /* scalability */
2354

  
2355
    ff_mpeg4_stuffing(&s->pb);
2356

  
2357
    /* user data */
2358
    if(!(s->flags & CODEC_FLAG_BITEXACT)){
2359
        put_bits(&s->pb, 16, 0);
2360
        put_bits(&s->pb, 16, 0x1B2);    /* user_data */
2361
        ff_put_string(&s->pb, LIBAVCODEC_IDENT, 0);
2197 2362
    }
2198 2363
}
2199 2364

  
2200
void h263_encode_init(MpegEncContext *s)
2365
/* write mpeg4 VOP header */
2366
void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
2201 2367
{
2202
    static int done = 0;
2368
    int time_incr;
2369
    int time_div, time_mod;
2203 2370

  
2204
    if (!done) {
2205
        done = 1;
2371
    if(s->pict_type==FF_I_TYPE){
2372
        if(!(s->flags&CODEC_FLAG_GLOBAL_HEADER)){
2373
            if(s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT) //HACK, the reference sw is buggy
2374
                mpeg4_encode_visual_object_header(s);
2375
            if(s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT || picture_number==0) //HACK, the reference sw is buggy
2376
                mpeg4_encode_vol_header(s, 0, 0);
2377
        }
2378
        if(!(s->workaround_bugs & FF_BUG_MS))
2379
            mpeg4_encode_gop_header(s);
2380
    }
2206 2381

  
2207
        init_uni_dc_tab();
2382
    s->partitioned_frame= s->data_partitioning && s->pict_type!=FF_B_TYPE;
2208 2383

  
2209
        init_rl(&rl_inter, static_rl_table_store[0]);
2210
        init_rl(&rl_intra, static_rl_table_store[1]);
2211
        init_rl(&rl_intra_aic, static_rl_table_store[2]);
2384
    put_bits(&s->pb, 16, 0);                /* vop header */
2385
    put_bits(&s->pb, 16, VOP_STARTCODE);    /* vop header */
2386
    put_bits(&s->pb, 2, s->pict_type - 1);  /* pict type: I = 0 , P = 1 */
2212 2387

  
2213
        init_uni_mpeg4_rl_tab(&rl_intra, uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len);
2214
        init_uni_mpeg4_rl_tab(&rl_inter, uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len);
2388
    assert(s->time>=0);
2389
    time_div= s->time/s->avctx->time_base.den;
2390
    time_mod= s->time%s->avctx->time_base.den;
2391
    time_incr= time_div - s->last_time_base;
2392
    assert(time_incr >= 0);
2393
    while(time_incr--)
2394
        put_bits(&s->pb, 1, 1);
2215 2395

  
2216
        init_uni_h263_rl_tab(&rl_intra_aic, NULL, uni_h263_intra_aic_rl_len);
2217
        init_uni_h263_rl_tab(&rl_inter    , NULL, uni_h263_inter_rl_len);
2396
    put_bits(&s->pb, 1, 0);
2218 2397

  
2219
        init_mv_penalty_and_fcode(s);
2398
    put_bits(&s->pb, 1, 1);                             /* marker */
2399
    put_bits(&s->pb, s->time_increment_bits, time_mod); /* time increment */
2400
    put_bits(&s->pb, 1, 1);                             /* marker */
2401
    put_bits(&s->pb, 1, 1);                             /* vop coded */
2402
    if (    s->pict_type == FF_P_TYPE
2403
        || (s->pict_type == FF_S_TYPE && s->vol_sprite_usage==GMC_SPRITE)) {
2404
        put_bits(&s->pb, 1, s->no_rounding);    /* rounding type */
2220 2405
    }
2221
    s->me.mv_penalty= mv_penalty; //FIXME exact table for msmpeg4 & h263p
2222

  
2223
    s->intra_ac_vlc_length     =s->inter_ac_vlc_length     = uni_h263_inter_rl_len;
2224
    s->intra_ac_vlc_last_length=s->inter_ac_vlc_last_length= uni_h263_inter_rl_len + 128*64;
2225
    if(s->h263_aic){
2226
        s->intra_ac_vlc_length     = uni_h263_intra_aic_rl_len;
2227
        s->intra_ac_vlc_last_length= uni_h263_intra_aic_rl_len + 128*64;
2406
    put_bits(&s->pb, 3, 0);     /* intra dc VLC threshold */
2407
    if(!s->progressive_sequence){
2408
         put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
2409
         put_bits(&s->pb, 1, s->alternate_scan);
2228 2410
    }
2229
    s->ac_esc_length= 7+1+6+8;
2411
    //FIXME sprite stuff
2230 2412

  
2231
    // use fcodes >1 only for mpeg4 & h263 & h263p FIXME
2232
    switch(s->codec_id){
2233
    case CODEC_ID_MPEG4:
2234
        s->fcode_tab= fcode_tab;
2235
        s->min_qcoeff= -2048;
2236
        s->max_qcoeff=  2047;
2237
        s->intra_ac_vlc_length     = uni_mpeg4_intra_rl_len;
2238
        s->intra_ac_vlc_last_length= uni_mpeg4_intra_rl_len + 128*64;
2239
        s->inter_ac_vlc_length     = uni_mpeg4_inter_rl_len;
2240
        s->inter_ac_vlc_last_length= uni_mpeg4_inter_rl_len + 128*64;
2241
        s->luma_dc_vlc_length= uni_DCtab_lum_len;
2242
        s->chroma_dc_vlc_length= uni_DCtab_chrom_len;
2243
        s->ac_esc_length= 7+2+1+6+1+12+1;
2244
        s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
2245
        s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
2413
    put_bits(&s->pb, 5, s->qscale);
2246 2414

  
2247
        if(s->flags & CODEC_FLAG_GLOBAL_HEADER){
2415
    if (s->pict_type != FF_I_TYPE)
2416
        put_bits(&s->pb, 3, s->f_code); /* fcode_for */
2417
    if (s->pict_type == FF_B_TYPE)
2418
        put_bits(&s->pb, 3, s->b_code); /* fcode_back */
2419
}
2248 2420

  
2249
            s->avctx->extradata= av_malloc(1024);
2250
            init_put_bits(&s->pb, s->avctx->extradata, 1024);
2421
void ff_h263_encode_motion(MpegEncContext * s, int val, int f_code)
2422
{
2423
    int range, l, bit_size, sign, code, bits;
2251 2424

  
2252
            if(!(s->workaround_bugs & FF_BUG_MS))
2253
                mpeg4_encode_visual_object_header(s);
2254
            mpeg4_encode_vol_header(s, 0, 0);
2425
    if (val == 0) {
2426
        /* zero vector */
2427
        code = 0;
2428
        put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
2429
    } else {
2430
        bit_size = f_code - 1;
2431
        range = 1 << bit_size;
2432
        /* modulo encoding */
2433
        l= INT_BIT - 6 - bit_size;
2434
        val = (val<<l)>>l;
2435
        sign = val>>31;
2436
        val= (val^sign)-sign;
2437
        sign&=1;
2255 2438

  
2256
//            ff_mpeg4_stuffing(&s->pb); ?
2257
            flush_put_bits(&s->pb);
2258
            s->avctx->extradata_size= (put_bits_count(&s->pb)+7)>>3;
2259
        }
2439
        val--;
2440
        code = (val >> bit_size) + 1;
2441
        bits = val & (range - 1);
2260 2442

  
2261
        break;
2262
    case CODEC_ID_H263P:
2263
        if(s->umvplus)
2264
            s->fcode_tab= umv_fcode_tab;
2265
        if(s->modified_quant){
2266
            s->min_qcoeff= -2047;
2267
            s->max_qcoeff=  2047;
2268
        }else{
2269
            s->min_qcoeff= -127;
2270
            s->max_qcoeff=  127;
2271
        }
2272
        break;
2273
        //Note for mpeg4 & h263 the dc-scale table will be set per frame as needed later
2274
    case CODEC_ID_FLV1:
2275
        if (s->h263_flv > 1) {
2276
            s->min_qcoeff= -1023;
2277
            s->max_qcoeff=  1023;
2278
        } else {
2279
            s->min_qcoeff= -127;
2280
            s->max_qcoeff=  127;
2443
        put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
2444
        if (bit_size > 0) {
2445
            put_bits(&s->pb, bit_size, bits);
2281 2446
        }
2282
        s->y_dc_scale_table=
2283
        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
2284
        break;
2285
    default: //nothing needed - default table already set in mpegvideo.c
2286
        s->min_qcoeff= -127;
2287
        s->max_qcoeff=  127;
2288
        s->y_dc_scale_table=
2289
        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
2290 2447
    }
2291 2448
}
2292 2449

  
2293
/***************************************************/
2294
/**
2295
 * add mpeg4 stuffing bits (01...1)
2296
 */
2297
void ff_mpeg4_stuffing(PutBitContext * pbc)
2450
static void init_mv_penalty_and_fcode(MpegEncContext *s)
2298 2451
{
2299
    int length;
2300
    put_bits(pbc, 1, 0);
2301
    length= (-put_bits_count(pbc))&7;
2302
    if(length) put_bits(pbc, length, (1<<length)-1);
2303
}
2304

  
2305
/* must be called before writing the header */
2306
void ff_set_mpeg4_time(MpegEncContext * s){
2307
    if(s->pict_type==FF_B_TYPE){
2308
        ff_mpeg4_init_direct_mv(s);
2309
    }else{
2310
        s->last_time_base= s->time_base;
2311
        s->time_base= s->time/s->avctx->time_base.den;
2312
    }
2313
}
2452
    int f_code;
2453
    int mv;
2314 2454

  
2315
static void mpeg4_encode_gop_header(MpegEncContext * s){
2316
    int hours, minutes, seconds;
2317
    int64_t time;
2455
    for(f_code=1; f_code<=MAX_FCODE; f_code++){
2456
        for(mv=-MAX_MV; mv<=MAX_MV; mv++){
2457
            int len;
2318 2458

  
2319
    put_bits(&s->pb, 16, 0);
2320
    put_bits(&s->pb, 16, GOP_STARTCODE);
2459
            if(mv==0) len= mvtab[0][1];
2460
            else{
2461
                int val, bit_size, code;
2321 2462

  
2322
    time= s->current_picture_ptr->pts;
2323
    if(s->reordered_input_picture[1])
2324
        time= FFMIN(time, s->reordered_input_picture[1]->pts);
2325
    time= time*s->avctx->time_base.num;
2463
                bit_size = f_code - 1;
2326 2464

  
2327
    seconds= time/s->avctx->time_base.den;
2328
    minutes= seconds/60; seconds %= 60;
2329
    hours= minutes/60; minutes %= 60;
2330
    hours%=24;
2331

  
2332
    put_bits(&s->pb, 5, hours);
2333
    put_bits(&s->pb, 6, minutes);
2334
    put_bits(&s->pb, 1, 1);
2335
    put_bits(&s->pb, 6, seconds);
2336

  
2337
    put_bits(&s->pb, 1, !!(s->flags&CODEC_FLAG_CLOSED_GOP));
2338
    put_bits(&s->pb, 1, 0); //broken link == NO
2339

  
2340
    s->last_time_base= time / s->avctx->time_base.den;
2341

  
2342
    ff_mpeg4_stuffing(&s->pb);
2343
}
2344

  
2345
static void mpeg4_encode_visual_object_header(MpegEncContext * s){
2346
    int profile_and_level_indication;
2347
    int vo_ver_id;
2465
                val=mv;
2466
                if (val < 0)
2467
                    val = -val;
2468
                val--;
2469
                code = (val >> bit_size) + 1;
2470
                if(code<33){
2471
                    len= mvtab[code][1] + 1 + bit_size;
2472
                }else{
2473
                    len= mvtab[32][1] + av_log2(code>>5) + 2 + bit_size;
2474
                }
2475
            }
2348 2476

  
2349
    if(s->avctx->profile != FF_PROFILE_UNKNOWN){
2350
        profile_and_level_indication = s->avctx->profile << 4;
2351
    }else if(s->max_b_frames || s->quarter_sample){
2352
        profile_and_level_indication= 0xF0; // adv simple
2353
    }else{
2354
        profile_and_level_indication= 0x00; // simple
2477
            mv_penalty[f_code][mv+MAX_MV]= len;
2478
        }
2355 2479
    }
2356 2480

  
2357
    if(s->avctx->level != FF_LEVEL_UNKNOWN){
2358
        profile_and_level_indication |= s->avctx->level;
2359
    }else{
2360
        profile_and_level_indication |= 1; //level 1
2481
    for(f_code=MAX_FCODE; f_code>0; f_code--){
2482
        for(mv=-(16<<f_code); mv<(16<<f_code); mv++){
2483
            fcode_tab[mv+MAX_MV]= f_code;
2484
        }
2361 2485
    }
2362 2486

  
2363
    if(profile_and_level_indication>>4 == 0xF){
2364
        vo_ver_id= 5;
2365
    }else{
2366
        vo_ver_id= 1;
2487
    for(mv=0; mv<MAX_MV*2+1; mv++){
2488
        umv_fcode_tab[mv]= 1;
2367 2489
    }
2490
}
2368 2491

  
2369
    //FIXME levels
2492
static void init_uni_dc_tab(void)
2493
{
2494
    int level, uni_code, uni_len;
2370 2495

  
2371
    put_bits(&s->pb, 16, 0);
2372
    put_bits(&s->pb, 16, VOS_STARTCODE);
2496
    for(level=-256; level<256; level++){
2497
        int size, v, l;
2498
        /* find number of bits */
2499
        size = 0;
2500
        v = abs(level);
2501
        while (v) {
2502
            v >>= 1;
2503
            size++;
2504
        }
2373 2505

  
2374
    put_bits(&s->pb, 8, profile_and_level_indication);
2506
        if (level < 0)
2507
            l= (-level) ^ ((1 << size) - 1);
2508
        else
2509
            l= level;
2375 2510

  
2376
    put_bits(&s->pb, 16, 0);
2377
    put_bits(&s->pb, 16, VISUAL_OBJ_STARTCODE);
2511
        /* luminance */
2512
        uni_code= DCtab_lum[size][0];
2513
        uni_len = DCtab_lum[size][1];
2378 2514

  
2379
    put_bits(&s->pb, 1, 1);
2380
        put_bits(&s->pb, 4, vo_ver_id);
2381
        put_bits(&s->pb, 3, 1); //priority
2515
        if (size > 0) {
2516
            uni_code<<=size; uni_code|=l;
2517
            uni_len+=size;
2518
            if (size > 8){
2519
                uni_code<<=1; uni_code|=1;
2520
                uni_len++;
2521
            }
2522
        }
2523
        uni_DCtab_lum_bits[level+256]= uni_code;
2524
        uni_DCtab_lum_len [level+256]= uni_len;
2382 2525

  
2383
    put_bits(&s->pb, 4, 1); //visual obj type== video obj
2526
        /* chrominance */
2527
        uni_code= DCtab_chrom[size][0];
2528
        uni_len = DCtab_chrom[size][1];
2384 2529

  
2385
    put_bits(&s->pb, 1, 0); //video signal type == no clue //FIXME
2530
        if (size > 0) {
2531
            uni_code<<=size; uni_code|=l;
2532
            uni_len+=size;
2533
            if (size > 8){
2534
                uni_code<<=1; uni_code|=1;
2535
                uni_len++;
2536
            }
2537
        }
2538
        uni_DCtab_chrom_bits[level+256]= uni_code;
2539
        uni_DCtab_chrom_len [level+256]= uni_len;
2386 2540

  
2387
    ff_mpeg4_stuffing(&s->pb);
2541
    }
2388 2542
}
2389 2543

  
2390
static void mpeg4_encode_vol_header(MpegEncContext * s, int vo_number, int vol_number)
2391
{
2392
    int vo_ver_id;
2393

  
2394
    if (!CONFIG_MPEG4_ENCODER)  return;
2544
static void init_uni_mpeg4_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab){
2545
    int slevel, run, last;
2395 2546

  
2396
    if(s->max_b_frames || s->quarter_sample){
2397
        vo_ver_id= 5;
2398
        s->vo_type= ADV_SIMPLE_VO_TYPE;
2399
    }else{
2400
        vo_ver_id= 1;
2401
        s->vo_type= SIMPLE_VO_TYPE;
2402
    }
2547
    assert(MAX_LEVEL >= 64);
2548
    assert(MAX_RUN   >= 63);
2403 2549

  
2404
    put_bits(&s->pb, 16, 0);
2405
    put_bits(&s->pb, 16, 0x100 + vo_number);        /* video obj */
2406
    put_bits(&s->pb, 16, 0);
2407
    put_bits(&s->pb, 16, 0x120 + vol_number);       /* video obj layer */
2550
    for(slevel=-64; slevel<64; slevel++){
2551
        if(slevel==0) continue;
2552
        for(run=0; run<64; run++){
2553
            for(last=0; last<=1; last++){
2554
                const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
2555
                int level= slevel < 0 ? -slevel : slevel;
2556
                int sign= slevel < 0 ? 1 : 0;
2557
                int bits, len, code;
2558
                int level1, run1;
2408 2559

  
2409
    put_bits(&s->pb, 1, 0);             /* random access vol */
2410
    put_bits(&s->pb, 8, s->vo_type);    /* video obj type indication */
2411
    if(s->workaround_bugs & FF_BUG_MS) {
2412
        put_bits(&s->pb, 1, 0);         /* is obj layer id= no */
2413
    } else {
2414
        put_bits(&s->pb, 1, 1);         /* is obj layer id= yes */
2415
        put_bits(&s->pb, 4, vo_ver_id); /* is obj layer ver id */
2416
        put_bits(&s->pb, 3, 1);         /* is obj layer priority */
2417
    }
2560
                len_tab[index]= 100;
2418 2561

  
2419
    aspect_to_info(s, s->avctx->sample_aspect_ratio);
2562
                /* ESC0 */
2563
                code= get_rl_index(rl, last, run, level);
2564
                bits= rl->table_vlc[code][0];
2565
                len=  rl->table_vlc[code][1];
2566
                bits=bits*2+sign; len++;
2420 2567

  
2421
    put_bits(&s->pb, 4, s->aspect_ratio_info);/* aspect ratio info */
2422
    if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){
2423
        put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
2424
        put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
2425
    }
2568
                if(code!=rl->n && len < len_tab[index]){
2569
                    bits_tab[index]= bits;
2570
                    len_tab [index]= len;
2571
                }
2572
                /* ESC1 */
2573
                bits= rl->table_vlc[rl->n][0];
2574
                len=  rl->table_vlc[rl->n][1];
2575
                bits=bits*2;    len++; //esc1
2576
                level1= level - rl->max_level[last][run];
2577
                if(level1>0){
2578
                    code= get_rl_index(rl, last, run, level1);
2579
                    bits<<= rl->table_vlc[code][1];
2580
                    len  += rl->table_vlc[code][1];
2581
                    bits += rl->table_vlc[code][0];
2582
                    bits=bits*2+sign; len++;
2426 2583

  
2427
    if(s->workaround_bugs & FF_BUG_MS) { //
2428
        put_bits(&s->pb, 1, 0);         /* vol control parameters= no @@@ */
2429
    } else {
2430
        put_bits(&s->pb, 1, 1);         /* vol control parameters= yes */
2431
        put_bits(&s->pb, 2, 1);         /* chroma format YUV 420/YV12 */
2432
        put_bits(&s->pb, 1, s->low_delay);
2433
        put_bits(&s->pb, 1, 0);         /* vbv parameters= no */
2434
    }
2584
                    if(code!=rl->n && len < len_tab[index]){
2585
                        bits_tab[index]= bits;
2586
                        len_tab [index]= len;
2587
                    }
2588
                }
2589
                /* ESC2 */
2590
                bits= rl->table_vlc[rl->n][0];
2591
                len=  rl->table_vlc[rl->n][1];
2592
                bits=bits*4+2;    len+=2; //esc2
2593
                run1 = run - rl->max_run[last][level] - 1;
2594
                if(run1>=0){
2595
                    code= get_rl_index(rl, last, run1, level);
2596
                    bits<<= rl->table_vlc[code][1];
2597
                    len  += rl->table_vlc[code][1];
2598
                    bits += rl->table_vlc[code][0];
2599
                    bits=bits*2+sign; len++;
2435 2600

  
2436
    put_bits(&s->pb, 2, RECT_SHAPE);    /* vol shape= rectangle */
2437
    put_bits(&s->pb, 1, 1);             /* marker bit */
2601
                    if(code!=rl->n && len < len_tab[index]){
2602
                        bits_tab[index]= bits;
2603
                        len_tab [index]= len;
2604
                    }
2605
                }
2606
                /* ESC3 */
2607
                bits= rl->table_vlc[rl->n][0];
2608
                len = rl->table_vlc[rl->n][1];
2609
                bits=bits*4+3;    len+=2; //esc3
2610
                bits=bits*2+last; len++;
2611
                bits=bits*64+run; len+=6;
2612
                bits=bits*2+1;    len++;  //marker
2613
                bits=bits*4096+(slevel&0xfff); len+=12;
2614
                bits=bits*2+1;    len++;  //marker
2438 2615

  
2439
    put_bits(&s->pb, 16, s->avctx->time_base.den);
2440
    if (s->time_increment_bits < 1)
2441
        s->time_increment_bits = 1;
2442
    put_bits(&s->pb, 1, 1);             /* marker bit */
2443
    put_bits(&s->pb, 1, 0);             /* fixed vop rate=no */
2444
    put_bits(&s->pb, 1, 1);             /* marker bit */
2445
    put_bits(&s->pb, 13, s->width);     /* vol width */
2446
    put_bits(&s->pb, 1, 1);             /* marker bit */
2447
    put_bits(&s->pb, 13, s->height);    /* vol height */
2448
    put_bits(&s->pb, 1, 1);             /* marker bit */
2449
    put_bits(&s->pb, 1, s->progressive_sequence ? 0 : 1);
2450
    put_bits(&s->pb, 1, 1);             /* obmc disable */
2451
    if (vo_ver_id == 1) {
2452
        put_bits(&s->pb, 1, s->vol_sprite_usage);       /* sprite enable */
2453
    }else{
2454
        put_bits(&s->pb, 2, s->vol_sprite_usage);       /* sprite enable */
2616
                if(len < len_tab[index]){
2617
                    bits_tab[index]= bits;
2618
                    len_tab [index]= len;
2619
                }
2620
            }
2621
        }
2455 2622
    }
2623
}
2456 2624

  
2457
    put_bits(&s->pb, 1, 0);             /* not 8 bit == false */
2458
    put_bits(&s->pb, 1, s->mpeg_quant); /* quant type= (0=h263 style)*/
2625
static void init_uni_h263_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab){
2626
    int slevel, run, last;
2459 2627

  
2460
    if(s->mpeg_quant){
2461
        ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
2462
        ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
2463
    }
2628
    assert(MAX_LEVEL >= 64);
2629
    assert(MAX_RUN   >= 63);
2464 2630

  
2465
    if (vo_ver_id != 1)
2466
        put_bits(&s->pb, 1, s->quarter_sample);
2467
    put_bits(&s->pb, 1, 1);             /* complexity estimation disable */
2468
    s->resync_marker= s->rtp_mode;
2469
    put_bits(&s->pb, 1, s->resync_marker ? 0 : 1);/* resync marker disable */
2470
    put_bits(&s->pb, 1, s->data_partitioning ? 1 : 0);
2471
    if(s->data_partitioning){
2472
        put_bits(&s->pb, 1, 0);         /* no rvlc */
2473
    }
2631
    for(slevel=-64; slevel<64; slevel++){
2632
        if(slevel==0) continue;
2633
        for(run=0; run<64; run++){
2634
            for(last=0; last<=1; last++){
2635
                const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
2636
                int level= slevel < 0 ? -slevel : slevel;
2637
                int sign= slevel < 0 ? 1 : 0;
2638
                int bits, len, code;
2474 2639

  
2475
    if (vo_ver_id != 1){
2476
        put_bits(&s->pb, 1, 0);         /* newpred */
2477
        put_bits(&s->pb, 1, 0);         /* reduced res vop */
2478
    }
2479
    put_bits(&s->pb, 1, 0);             /* scalability */
2640
                len_tab[index]= 100;
2480 2641

  
2481
    ff_mpeg4_stuffing(&s->pb);
2642
                /* ESC0 */
2643
                code= get_rl_index(rl, last, run, level);
2644
                bits= rl->table_vlc[code][0];
2645
                len=  rl->table_vlc[code][1];
2646
                bits=bits*2+sign; len++;
2482 2647

  
2483
    /* user data */
2484
    if(!(s->flags & CODEC_FLAG_BITEXACT)){
2485
        put_bits(&s->pb, 16, 0);
2486
        put_bits(&s->pb, 16, 0x1B2);    /* user_data */
2487
        ff_put_string(&s->pb, LIBAVCODEC_IDENT, 0);
2648
                if(code!=rl->n && len < len_tab[index]){
2649
                    if(bits_tab) bits_tab[index]= bits;
2650
                    len_tab [index]= len;
2651
                }
2652
                /* ESC */
2653
                bits= rl->table_vlc[rl->n][0];
2654
                len = rl->table_vlc[rl->n][1];
2655
                bits=bits*2+last; len++;
2656
                bits=bits*64+run; len+=6;
2657
                bits=bits*256+(level&0xff); len+=8;
2658

  
2659
                if(len < len_tab[index]){
2660
                    if(bits_tab) bits_tab[index]= bits;
2661
                    len_tab [index]= len;
2662
                }
2663
            }
2664
        }
2488 2665
    }
2489 2666
}
2490 2667

  
2491
/* write mpeg4 VOP header */
2492
void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
2668
void h263_encode_init(MpegEncContext *s)
2493 2669
{
2494
    int time_incr;
2495
    int time_div, time_mod;
2670
    static int done = 0;
2496 2671

  
2497
    if(s->pict_type==FF_I_TYPE){
2498
        if(!(s->flags&CODEC_FLAG_GLOBAL_HEADER)){
2499
            if(s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT) //HACK, the reference sw is buggy
2500
                mpeg4_encode_visual_object_header(s);
2501
            if(s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT || picture_number==0) //HACK, the reference sw is buggy
2502
                mpeg4_encode_vol_header(s, 0, 0);
2503
        }
2504
        if(!(s->workaround_bugs & FF_BUG_MS))
2505
            mpeg4_encode_gop_header(s);
2506
    }
2672
    if (!done) {
2673
        done = 1;
2507 2674

  
2508
    s->partitioned_frame= s->data_partitioning && s->pict_type!=FF_B_TYPE;
2675
        init_uni_dc_tab();
2509 2676

  
2510
    put_bits(&s->pb, 16, 0);                /* vop header */
2511
    put_bits(&s->pb, 16, VOP_STARTCODE);    /* vop header */
2512
    put_bits(&s->pb, 2, s->pict_type - 1);  /* pict type: I = 0 , P = 1 */
2677
        init_rl(&rl_inter, static_rl_table_store[0]);
2678
        init_rl(&rl_intra, static_rl_table_store[1]);
2679
        init_rl(&rl_intra_aic, static_rl_table_store[2]);
2513 2680

  
2514
    assert(s->time>=0);
2515
    time_div= s->time/s->avctx->time_base.den;
2516
    time_mod= s->time%s->avctx->time_base.den;
2517
    time_incr= time_div - s->last_time_base;
2518
    assert(time_incr >= 0);
2519
    while(time_incr--)
2520
        put_bits(&s->pb, 1, 1);
2681
        init_uni_mpeg4_rl_tab(&rl_intra, uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len);
2682
        init_uni_mpeg4_rl_tab(&rl_inter, uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len);
2521 2683

  
2522
    put_bits(&s->pb, 1, 0);
2684
        init_uni_h263_rl_tab(&rl_intra_aic, NULL, uni_h263_intra_aic_rl_len);
2685
        init_uni_h263_rl_tab(&rl_inter    , NULL, uni_h263_inter_rl_len);
2523 2686

  
2524
    put_bits(&s->pb, 1, 1);                             /* marker */
2525
    put_bits(&s->pb, s->time_increment_bits, time_mod); /* time increment */
2526
    put_bits(&s->pb, 1, 1);                             /* marker */
2527
    put_bits(&s->pb, 1, 1);                             /* vop coded */
2528
    if (    s->pict_type == FF_P_TYPE
2529
        || (s->pict_type == FF_S_TYPE && s->vol_sprite_usage==GMC_SPRITE)) {
2530
        put_bits(&s->pb, 1, s->no_rounding);    /* rounding type */
2687
        init_mv_penalty_and_fcode(s);
2531 2688
    }
2532
    put_bits(&s->pb, 3, 0);     /* intra dc VLC threshold */
2533
    if(!s->progressive_sequence){
2534
         put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
2535
         put_bits(&s->pb, 1, s->alternate_scan);
2689
    s->me.mv_penalty= mv_penalty; //FIXME exact table for msmpeg4 & h263p
2690

  
2691
    s->intra_ac_vlc_length     =s->inter_ac_vlc_length     = uni_h263_inter_rl_len;
2692
    s->intra_ac_vlc_last_length=s->inter_ac_vlc_last_length= uni_h263_inter_rl_len + 128*64;
2693
    if(s->h263_aic){
2694
        s->intra_ac_vlc_length     = uni_h263_intra_aic_rl_len;
2695
        s->intra_ac_vlc_last_length= uni_h263_intra_aic_rl_len + 128*64;
2696
    }
2697
    s->ac_esc_length= 7+1+6+8;
2698

  
2699
    // use fcodes >1 only for mpeg4 & h263 & h263p FIXME
2700
    switch(s->codec_id){
2701
    case CODEC_ID_MPEG4:
2702
        s->fcode_tab= fcode_tab;
2703
        s->min_qcoeff= -2048;
2704
        s->max_qcoeff=  2047;
2705
        s->intra_ac_vlc_length     = uni_mpeg4_intra_rl_len;
2706
        s->intra_ac_vlc_last_length= uni_mpeg4_intra_rl_len + 128*64;
2707
        s->inter_ac_vlc_length     = uni_mpeg4_inter_rl_len;
2708
        s->inter_ac_vlc_last_length= uni_mpeg4_inter_rl_len + 128*64;
2709
        s->luma_dc_vlc_length= uni_DCtab_lum_len;
2710
        s->chroma_dc_vlc_length= uni_DCtab_chrom_len;
2711
        s->ac_esc_length= 7+2+1+6+1+12+1;
2712
        s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
2713
        s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
2714

  
2715
        if(s->flags & CODEC_FLAG_GLOBAL_HEADER){
2716

  
2717
            s->avctx->extradata= av_malloc(1024);
2718
            init_put_bits(&s->pb, s->avctx->extradata, 1024);
2719

  
2720
            if(!(s->workaround_bugs & FF_BUG_MS))
2721
                mpeg4_encode_visual_object_header(s);
2722
            mpeg4_encode_vol_header(s, 0, 0);
2723

  
2724
//            ff_mpeg4_stuffing(&s->pb); ?
2725
            flush_put_bits(&s->pb);
2726
            s->avctx->extradata_size= (put_bits_count(&s->pb)+7)>>3;
2727
        }
2728

  
2729
        break;
2730
    case CODEC_ID_H263P:
2731
        if(s->umvplus)
2732
            s->fcode_tab= umv_fcode_tab;
2733
        if(s->modified_quant){
2734
            s->min_qcoeff= -2047;
2735
            s->max_qcoeff=  2047;
2736
        }else{
2737
            s->min_qcoeff= -127;
2738
            s->max_qcoeff=  127;
2739
        }
2740
        break;
2741
        //Note for mpeg4 & h263 the dc-scale table will be set per frame as needed later
2742
    case CODEC_ID_FLV1:
2743
        if (s->h263_flv > 1) {
2744
            s->min_qcoeff= -1023;
2745
            s->max_qcoeff=  1023;
2746
        } else {
2747
            s->min_qcoeff= -127;
2748
            s->max_qcoeff=  127;
2749
        }
2750
        s->y_dc_scale_table=
2751
        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
2752
        break;
2753
    default: //nothing needed - default table already set in mpegvideo.c
2754
        s->min_qcoeff= -127;
2755
        s->max_qcoeff=  127;
2756
        s->y_dc_scale_table=
2757
        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
2536 2758
    }
2537
    //FIXME sprite stuff
2538

  
2539
    put_bits(&s->pb, 5, s->qscale);
2540

  
2541
    if (s->pict_type != FF_I_TYPE)
2542
        put_bits(&s->pb, 3, s->f_code); /* fcode_for */
2543
    if (s->pict_type == FF_B_TYPE)
2544
        put_bits(&s->pb, 3, s->b_code); /* fcode_back */
2545 2759
}
2546

  
2547 2760
#endif //CONFIG_ENCODERS
2548 2761

  
2549 2762
/**
......
2606 2819

  
2607 2820
}
2608 2821

  
2609
#if CONFIG_ENCODERS
2610

  
2611
/**
2612
 * encodes the dc value.
2613
 * @param n block index (0-3 are luma, 4-5 are chroma)
2614
 */
2615
static inline void mpeg4_encode_dc(PutBitContext * s, int level, int n)
2616
{
2617
#if 1
2618
    level+=256;
2619
    if (n < 4) {
2620
        /* luminance */
2621
        put_bits(s, uni_DCtab_lum_len[level], uni_DCtab_lum_bits[level]);
2622
    } else {
2623
        /* chrominance */
2624
        put_bits(s, uni_DCtab_chrom_len[level], uni_DCtab_chrom_bits[level]);
2625
    }
2626
#else
2627
    int size, v;
2628
    /* find number of bits */
2629
    size = 0;
2630
    v = abs(level);
2631
    while (v) {
2632
        v >>= 1;
2633
        size++;
2634
    }
2635

  
2636
    if (n < 4) {
2637
        /* luminance */
2638
        put_bits(&s->pb, DCtab_lum[size][1], DCtab_lum[size][0]);
2639
    } else {
2640
        /* chrominance */
2641
        put_bits(&s->pb, DCtab_chrom[size][1], DCtab_chrom[size][0]);
2642
    }
2643

  
2644
    /* encode remaining bits */
2645
    if (size > 0) {
2646
        if (level < 0)
2647
            level = (-level) ^ ((1 << size) - 1);
2648
        put_bits(&s->pb, size, level);
2649
        if (size > 8)
2650
            put_bits(&s->pb, 1, 1);
2651
    }
2652
#endif
2653
}
2654

  
2655
static inline int mpeg4_get_dc_length(int level, int n){
2656
    if (n < 4) {
2657
        return uni_DCtab_lum_len[level + 256];
2658
    } else {
2659
        return uni_DCtab_chrom_len[level + 256];
2660
    }
2661
}
2662

  
2663
/**
2664
 * encodes a 8x8 block
2665
 * @param n block index (0-3 are luma, 4-5 are chroma)
2666
 */
2667
static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n, int intra_dc,
2668
                               uint8_t *scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb)
2669
{
2670
    int i, last_non_zero;
2671
#if 0 //variables for the outcommented version
2672
    int code, sign, last;
2673
#endif
2674
    const RLTable *rl;
2675
    uint32_t *bits_tab;
2676
    uint8_t *len_tab;
2677
    const int last_index = s->block_last_index[n];
2678

  
2679
    if (s->mb_intra) { //Note gcc (3.2.1 at least) will optimize this away
2680
        /* mpeg4 based DC predictor */
2681
        mpeg4_encode_dc(dc_pb, intra_dc, n);
2682
        if(last_index<1) return;
2683
        i = 1;
2684
        rl = &rl_intra;
2685
        bits_tab= uni_mpeg4_intra_rl_bits;
2686
        len_tab = uni_mpeg4_intra_rl_len;
2687
    } else {
2688
        if(last_index<0) return;
2689
        i = 0;
2690
        rl = &rl_inter;
2691
        bits_tab= uni_mpeg4_inter_rl_bits;
2692
        len_tab = uni_mpeg4_inter_rl_len;
2693
    }
2694

  
2695
    /* AC coefs */
2696
    last_non_zero = i - 1;
2697
#if 1
2698
    for (; i < last_index; i++) {
2699
        int level = block[ scan_table[i] ];
2700
        if (level) {
2701
            int run = i - last_non_zero - 1;
2702
            level+=64;
2703
            if((level&(~127)) == 0){
2704
                const int index= UNI_MPEG4_ENC_INDEX(0, run, level);
2705
                put_bits(ac_pb, len_tab[index], bits_tab[index]);
2706
            }else{ //ESC3
2707
                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);
2708
            }
2709
            last_non_zero = i;
2710
        }
2711
    }
2712
    /*if(i<=last_index)*/{
2713
        int level = block[ scan_table[i] ];
2714
        int run = i - last_non_zero - 1;
2715
        level+=64;
2716
        if((level&(~127)) == 0){
2717
            const int index= UNI_MPEG4_ENC_INDEX(1, run, level);
2718
            put_bits(ac_pb, len_tab[index], bits_tab[index]);
2719
        }else{ //ESC3
2720
            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);
2721
        }
2722
    }
2723
#else
2724
    for (; i <= last_index; i++) {
2725
        const int slevel = block[ scan_table[i] ];
2726
        if (slevel) {
2727
            int level;
2728
            int run = i - last_non_zero - 1;
2729
            last = (i == last_index);
2730
            sign = 0;
2731
            level = slevel;
2732
            if (level < 0) {
2733
                sign = 1;
2734
                level = -level;
2735
            }
2736
            code = get_rl_index(rl, last, run, level);
2737
            put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
2738
            if (code == rl->n) {
2739
                int level1, run1;
2740
                level1 = level - rl->max_level[last][run];
2741
                if (level1 < 1)
2742
                    goto esc2;
2743
                code = get_rl_index(rl, last, run, level1);
2744
                if (code == rl->n) {
2745
                esc2:
2746
                    put_bits(ac_pb, 1, 1);
2747
                    if (level > MAX_LEVEL)
2748
                        goto esc3;
2749
                    run1 = run - rl->max_run[last][level] - 1;
2750
                    if (run1 < 0)
2751
                        goto esc3;
2752
                    code = get_rl_index(rl, last, run1, level);
2753
                    if (code == rl->n) {
2754
                    esc3:
2755
                        /* third escape */
2756
                        put_bits(ac_pb, 1, 1);
2757
                        put_bits(ac_pb, 1, last);
2758
                        put_bits(ac_pb, 6, run);
2759
                        put_bits(ac_pb, 1, 1);
2760
                        put_sbits(ac_pb, 12, slevel);
2761
                        put_bits(ac_pb, 1, 1);
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff