msmpeg4.c 9.67 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1
/*
2
 * MSMPEG4 backend for encoder and decoder
3
 * Copyright (c) 2001 Fabrice Bellard
4
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
Fabrice Bellard's avatar
Fabrice Bellard committed
5
 *
6 7
 * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
 *
8 9 10
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
11 12
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
13
 * version 2.1 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
14
 *
15
 * FFmpeg is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Fabrice Bellard's avatar
Fabrice Bellard committed
19
 *
20
 * You should have received a copy of the GNU Lesser General Public
21
 * License along with FFmpeg; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Fabrice Bellard's avatar
Fabrice Bellard committed
23
 */
Michael Niedermayer's avatar
Michael Niedermayer committed
24 25

/**
26
 * @file
27
 * MSMPEG4 backend for encoder and decoder
Michael Niedermayer's avatar
Michael Niedermayer committed
28 29
 */

30
#include "avcodec.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
31 32
#include "dsputil.h"
#include "mpegvideo.h"
33
#include "msmpeg4.h"
34
#include "libavutil/x86/asm.h"
35 36
#include "h263.h"
#include "mpeg4video.h"
37
#include "msmpeg4data.h"
38
#include "vc1data.h"
39
#include "libavutil/imgutils.h"
40

Fabrice Bellard's avatar
Fabrice Bellard committed
41
/*
42
 * You can also call this codec : MPEG4 with a twist !
Fabrice Bellard's avatar
Fabrice Bellard committed
43
 *
44
 * TODO:
Fabrice Bellard's avatar
Fabrice Bellard committed
45
 *        - (encoding) select best mv table (two choices)
46
 *        - (encoding) select best vlc/dc table
Fabrice Bellard's avatar
Fabrice Bellard committed
47 48
 */

49 50 51 52 53 54
/* This table is practically identical to the one from h263
 * except that it is inverted. */
static av_cold void init_h263_dc_for_msmpeg4(void)
{
        int level, uni_code, uni_len;

55 56 57
        if(ff_v2_dc_chroma_table[255 + 256][1])
            return;

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        for(level=-256; level<256; level++){
            int size, v, l;
            /* find number of bits */
            size = 0;
            v = abs(level);
            while (v) {
                v >>= 1;
                    size++;
            }

            if (level < 0)
                l= (-level) ^ ((1 << size) - 1);
            else
                l= level;

            /* luminance h263 */
74 75
            uni_code= ff_mpeg4_DCtab_lum[size][0];
            uni_len = ff_mpeg4_DCtab_lum[size][1];
76 77 78 79 80 81 82 83 84 85
            uni_code ^= (1<<uni_len)-1; //M$ does not like compatibility

            if (size > 0) {
                uni_code<<=size; uni_code|=l;
                uni_len+=size;
                if (size > 8){
                    uni_code<<=1; uni_code|=1;
                    uni_len++;
                }
            }
86 87
            ff_v2_dc_lum_table[level + 256][0] = uni_code;
            ff_v2_dc_lum_table[level + 256][1] = uni_len;
88 89

            /* chrominance h263 */
90 91
            uni_code= ff_mpeg4_DCtab_chrom[size][0];
            uni_len = ff_mpeg4_DCtab_chrom[size][1];
92 93 94 95 96 97 98 99 100 101
            uni_code ^= (1<<uni_len)-1; //M$ does not like compatibility

            if (size > 0) {
                uni_code<<=size; uni_code|=l;
                uni_len+=size;
                if (size > 8){
                    uni_code<<=1; uni_code|=1;
                    uni_len++;
                }
            }
102 103
            ff_v2_dc_chroma_table[level + 256][0] = uni_code;
            ff_v2_dc_chroma_table[level + 256][1] = uni_len;
104 105 106 107

        }
}

108
av_cold void ff_msmpeg4_common_init(MpegEncContext *s)
Michael Niedermayer's avatar
Michael Niedermayer committed
109 110 111 112 113 114 115 116 117
{
    switch(s->msmpeg4_version){
    case 1:
    case 2:
        s->y_dc_scale_table=
        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
        break;
    case 3:
        if(s->workaround_bugs){
118 119
            s->y_dc_scale_table= ff_old_ff_y_dc_scale_table;
            s->c_dc_scale_table= ff_wmv1_c_dc_scale_table;
Michael Niedermayer's avatar
Michael Niedermayer committed
120 121 122 123 124 125
        } else{
            s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
            s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
        }
        break;
    case 4:
Michael Niedermayer's avatar
Michael Niedermayer committed
126
    case 5:
127 128
        s->y_dc_scale_table= ff_wmv1_y_dc_scale_table;
        s->c_dc_scale_table= ff_wmv1_c_dc_scale_table;
Michael Niedermayer's avatar
Michael Niedermayer committed
129
        break;
130
#if CONFIG_VC1_DECODER
anonymous's avatar
anonymous committed
131
    case 6:
132 133
        s->y_dc_scale_table= ff_wmv3_dc_scale_table;
        s->c_dc_scale_table= ff_wmv3_dc_scale_table;
anonymous's avatar
anonymous committed
134
        break;
135
#endif
anonymous's avatar
anonymous committed
136

Michael Niedermayer's avatar
Michael Niedermayer committed
137 138
    }

139

Michael Niedermayer's avatar
Michael Niedermayer committed
140
    if(s->msmpeg4_version>=4){
141 142 143 144
        ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_wmv1_scantable[1]);
        ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_wmv1_scantable[2]);
        ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_wmv1_scantable[3]);
        ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_wmv1_scantable[0]);
Michael Niedermayer's avatar
Michael Niedermayer committed
145
    }
146
    //Note the default tables are set in common_init in mpegvideo.c
147

148
    init_h263_dc_for_msmpeg4();
Michael Niedermayer's avatar
Michael Niedermayer committed
149 150
}

Fabrice Bellard's avatar
Fabrice Bellard committed
151
/* predict coded block */
152
int ff_msmpeg4_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
Fabrice Bellard's avatar
Fabrice Bellard committed
153
{
154
    int xy, wrap, pred, a, b, c;
Fabrice Bellard's avatar
Fabrice Bellard committed
155

156
    xy = s->block_index[n];
157
    wrap = s->b8_stride;
Fabrice Bellard's avatar
Fabrice Bellard committed
158 159

    /* B C
160
     * A X
Fabrice Bellard's avatar
Fabrice Bellard committed
161
     */
162 163 164
    a = s->coded_block[xy - 1       ];
    b = s->coded_block[xy - 1 - wrap];
    c = s->coded_block[xy     - wrap];
165

Fabrice Bellard's avatar
Fabrice Bellard committed
166 167 168 169 170
    if (b == c) {
        pred = a;
    } else {
        pred = c;
    }
171

Fabrice Bellard's avatar
Fabrice Bellard committed
172
    /* store value */
173
    *coded_block_ptr = &s->coded_block[xy];
Fabrice Bellard's avatar
Fabrice Bellard committed
174 175 176 177

    return pred;
}

178 179 180 181 182 183 184 185 186 187
static int get_dc(uint8_t *src, int stride, int scale)
{
    int y;
    int sum=0;
    for(y=0; y<8; y++){
        int x;
        for(x=0; x<8; x++){
            sum+=src[x + y*stride];
        }
    }
188
    return FASTDIV((sum + (scale>>1)), scale);
189 190
}

Fabrice Bellard's avatar
Fabrice Bellard committed
191
/* dir = 0: left, dir = 1: top prediction */
192 193
int ff_msmpeg4_pred_dc(MpegEncContext *s, int n,
                       int16_t **dc_val_ptr, int *dir_ptr)
Fabrice Bellard's avatar
Fabrice Bellard committed
194
{
195
    int a, b, c, wrap, pred, scale;
196
    int16_t *dc_val;
Fabrice Bellard's avatar
Fabrice Bellard committed
197 198 199

    /* find prediction */
    if (n < 4) {
200
        scale = s->y_dc_scale;
Fabrice Bellard's avatar
Fabrice Bellard committed
201
    } else {
202
        scale = s->c_dc_scale;
Fabrice Bellard's avatar
Fabrice Bellard committed
203
    }
204

205 206
    wrap = s->block_wrap[n];
    dc_val= s->dc_val[0] + s->block_index[n];
Fabrice Bellard's avatar
Fabrice Bellard committed
207 208

    /* B C
209
     * A X
Fabrice Bellard's avatar
Fabrice Bellard committed
210
     */
211 212 213
    a = dc_val[ - 1];
    b = dc_val[ - 1 - wrap];
    c = dc_val[ - wrap];
214

Michael Niedermayer's avatar
Michael Niedermayer committed
215
    if(s->first_slice_line && (n&2)==0 && s->msmpeg4_version<4){
216 217
        b=c=1024;
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
218 219 220 221 222

    /* XXX: the following solution consumes divisions, but it does not
       necessitate to modify mpegvideo.c. The problem comes from the
       fact they decided to store the quantized DC (which would lead
       to problems if Q could vary !) */
223
#if ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE
224
    __asm__ volatile(
225 226 227 228 229
        "movl %3, %%eax         \n\t"
        "shrl $1, %%eax         \n\t"
        "addl %%eax, %2         \n\t"
        "addl %%eax, %1         \n\t"
        "addl %0, %%eax         \n\t"
230
        "imull %4               \n\t"
231 232
        "movl %%edx, %0         \n\t"
        "movl %1, %%eax         \n\t"
233
        "imull %4               \n\t"
234 235
        "movl %%edx, %1         \n\t"
        "movl %2, %%eax         \n\t"
236
        "imull %4               \n\t"
237 238
        "movl %%edx, %2         \n\t"
        : "+b" (a), "+c" (b), "+D" (c)
239
        : "g" (scale), "S" (ff_inverse[scale])
240
        : "%eax", "%edx"
241
    );
242
#else
243
    /* #elif ARCH_ALPHA */
244
    /* Divisions are extremely costly on Alpha; optimize the most
245 246
       common case. But they are costly everywhere...
     */
247
    if (scale == 8) {
248 249 250
        a = (a + (8 >> 1)) / 8;
        b = (b + (8 >> 1)) / 8;
        c = (c + (8 >> 1)) / 8;
251
    } else {
252 253 254
        a = FASTDIV((a + (scale >> 1)), scale);
        b = FASTDIV((b + (scale >> 1)), scale);
        c = FASTDIV((c + (scale >> 1)), scale);
255
    }
256
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
257 258
    /* XXX: WARNING: they did not choose the same test as MPEG4. This
       is very important ! */
259
    if(s->msmpeg4_version>3){
260 261 262
        if(s->inter_intra_pred){
            uint8_t *dest;
            int wrap;
263

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
            if(n==1){
                pred=a;
                *dir_ptr = 0;
            }else if(n==2){
                pred=c;
                *dir_ptr = 1;
            }else if(n==3){
                if (abs(a - b) < abs(b - c)) {
                    pred = c;
                    *dir_ptr = 1;
                } else {
                    pred = a;
                    *dir_ptr = 0;
                }
            }else{
                if(n<4){
                    wrap= s->linesize;
281
                    dest= s->current_picture.f.data[0] + (((n >> 1) + 2*s->mb_y) * 8*  wrap ) + ((n & 1) + 2*s->mb_x) * 8;
282
                }else{
Michael Niedermayer's avatar
Michael Niedermayer committed
283
                    wrap= s->uvlinesize;
284
                    dest= s->current_picture.f.data[n - 3] + (s->mb_y * 8 * wrap) + s->mb_x * 8;
285 286 287 288 289
                }
                if(s->mb_x==0) a= (1024 + (scale>>1))/scale;
                else           a= get_dc(dest-8, wrap, scale*8);
                if(s->mb_y==0) c= (1024 + (scale>>1))/scale;
                else           c= get_dc(dest-8*wrap, wrap, scale*8);
290

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
                if (s->h263_aic_dir==0) {
                    pred= a;
                    *dir_ptr = 0;
                }else if (s->h263_aic_dir==1) {
                    if(n==0){
                        pred= c;
                        *dir_ptr = 1;
                    }else{
                        pred= a;
                        *dir_ptr = 0;
                    }
                }else if (s->h263_aic_dir==2) {
                    if(n==0){
                        pred= a;
                        *dir_ptr = 0;
                    }else{
                        pred= c;
                        *dir_ptr = 1;
                    }
                } else {
                    pred= c;
                    *dir_ptr = 1;
                }
            }
        }else{
            if (abs(a - b) < abs(b - c)) {
                pred = c;
                *dir_ptr = 1;
            } else {
                pred = a;
                *dir_ptr = 0;
            }
323 324 325 326 327 328 329 330 331
        }
    }else{
        if (abs(a - b) <= abs(b - c)) {
            pred = c;
            *dir_ptr = 1;
        } else {
            pred = a;
            *dir_ptr = 0;
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
332 333 334
    }

    /* update predictor */
335
    *dc_val_ptr = &dc_val[0];
Fabrice Bellard's avatar
Fabrice Bellard committed
336 337 338
    return pred;
}