]> git.sesse.net Git - ffmpeg/blob - libavcodec/msmpeg4.c
x86: sbrdsp: Implement SSE2 qmf_deint_bfly
[ffmpeg] / libavcodec / msmpeg4.c
1 /*
2  * MSMPEG4 backend for encoder and decoder
3  * Copyright (c) 2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
7  *
8  * This file is part of Libav.
9  *
10  * Libav is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * Libav is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with Libav; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24
25 /**
26  * @file
27  * MSMPEG4 backend for encoder and decoder
28  */
29
30 #include "avcodec.h"
31 #include "dsputil.h"
32 #include "mpegvideo.h"
33 #include "msmpeg4.h"
34 #include "libavutil/x86/asm.h"
35 #include "h263.h"
36 #include "mpeg4video.h"
37 #include "msmpeg4data.h"
38 #include "vc1data.h"
39
40 /*
41  * You can also call this codec : MPEG4 with a twist !
42  *
43  * TODO:
44  *        - (encoding) select best mv table (two choices)
45  *        - (encoding) select best vlc/dc table
46  */
47 //#define DEBUG
48
49 /* This table is practically identical to the one from h263
50  * except that it is inverted. */
51 static av_cold void init_h263_dc_for_msmpeg4(void)
52 {
53         int level, uni_code, uni_len;
54
55         for(level=-256; level<256; level++){
56             int size, v, l;
57             /* find number of bits */
58             size = 0;
59             v = abs(level);
60             while (v) {
61                 v >>= 1;
62                     size++;
63             }
64
65             if (level < 0)
66                 l= (-level) ^ ((1 << size) - 1);
67             else
68                 l= level;
69
70             /* luminance h263 */
71             uni_code= ff_mpeg4_DCtab_lum[size][0];
72             uni_len = ff_mpeg4_DCtab_lum[size][1];
73             uni_code ^= (1<<uni_len)-1; //M$ does not like compatibility
74
75             if (size > 0) {
76                 uni_code<<=size; uni_code|=l;
77                 uni_len+=size;
78                 if (size > 8){
79                     uni_code<<=1; uni_code|=1;
80                     uni_len++;
81                 }
82             }
83             ff_v2_dc_lum_table[level + 256][0] = uni_code;
84             ff_v2_dc_lum_table[level + 256][1] = uni_len;
85
86             /* chrominance h263 */
87             uni_code= ff_mpeg4_DCtab_chrom[size][0];
88             uni_len = ff_mpeg4_DCtab_chrom[size][1];
89             uni_code ^= (1<<uni_len)-1; //M$ does not like compatibility
90
91             if (size > 0) {
92                 uni_code<<=size; uni_code|=l;
93                 uni_len+=size;
94                 if (size > 8){
95                     uni_code<<=1; uni_code|=1;
96                     uni_len++;
97                 }
98             }
99             ff_v2_dc_chroma_table[level + 256][0] = uni_code;
100             ff_v2_dc_chroma_table[level + 256][1] = uni_len;
101
102         }
103 }
104
105 av_cold void ff_msmpeg4_common_init(MpegEncContext *s)
106 {
107     static int initialized=0;
108
109     switch(s->msmpeg4_version){
110     case 1:
111     case 2:
112         s->y_dc_scale_table=
113         s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
114         break;
115     case 3:
116         if(s->workaround_bugs){
117             s->y_dc_scale_table= ff_old_ff_y_dc_scale_table;
118             s->c_dc_scale_table= ff_wmv1_c_dc_scale_table;
119         } else{
120             s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
121             s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
122         }
123         break;
124     case 4:
125     case 5:
126         s->y_dc_scale_table= ff_wmv1_y_dc_scale_table;
127         s->c_dc_scale_table= ff_wmv1_c_dc_scale_table;
128         break;
129 #if CONFIG_VC1_DECODER
130     case 6:
131         s->y_dc_scale_table= ff_wmv3_dc_scale_table;
132         s->c_dc_scale_table= ff_wmv3_dc_scale_table;
133         break;
134 #endif
135
136     }
137
138
139     if(s->msmpeg4_version>=4){
140         ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_wmv1_scantable[1]);
141         ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_wmv1_scantable[2]);
142         ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_wmv1_scantable[3]);
143         ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_wmv1_scantable[0]);
144     }
145     //Note the default tables are set in common_init in mpegvideo.c
146
147     if(!initialized){
148         initialized=1;
149
150         init_h263_dc_for_msmpeg4();
151     }
152 }
153
154 /* predict coded block */
155 int ff_msmpeg4_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
156 {
157     int xy, wrap, pred, a, b, c;
158
159     xy = s->block_index[n];
160     wrap = s->b8_stride;
161
162     /* B C
163      * A X
164      */
165     a = s->coded_block[xy - 1       ];
166     b = s->coded_block[xy - 1 - wrap];
167     c = s->coded_block[xy     - wrap];
168
169     if (b == c) {
170         pred = a;
171     } else {
172         pred = c;
173     }
174
175     /* store value */
176     *coded_block_ptr = &s->coded_block[xy];
177
178     return pred;
179 }
180
181 static int get_dc(uint8_t *src, int stride, int scale)
182 {
183     int y;
184     int sum=0;
185     for(y=0; y<8; y++){
186         int x;
187         for(x=0; x<8; x++){
188             sum+=src[x + y*stride];
189         }
190     }
191     return FASTDIV((sum + (scale>>1)), scale);
192 }
193
194 /* dir = 0: left, dir = 1: top prediction */
195 int ff_msmpeg4_pred_dc(MpegEncContext *s, int n,
196                        int16_t **dc_val_ptr, int *dir_ptr)
197 {
198     int a, b, c, wrap, pred, scale;
199     int16_t *dc_val;
200
201     /* find prediction */
202     if (n < 4) {
203         scale = s->y_dc_scale;
204     } else {
205         scale = s->c_dc_scale;
206     }
207
208     wrap = s->block_wrap[n];
209     dc_val= s->dc_val[0] + s->block_index[n];
210
211     /* B C
212      * A X
213      */
214     a = dc_val[ - 1];
215     b = dc_val[ - 1 - wrap];
216     c = dc_val[ - wrap];
217
218     if(s->first_slice_line && (n&2)==0 && s->msmpeg4_version<4){
219         b=c=1024;
220     }
221
222     /* XXX: the following solution consumes divisions, but it does not
223        necessitate to modify mpegvideo.c. The problem comes from the
224        fact they decided to store the quantized DC (which would lead
225        to problems if Q could vary !) */
226 #if ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE
227     __asm__ volatile(
228         "movl %3, %%eax         \n\t"
229         "shrl $1, %%eax         \n\t"
230         "addl %%eax, %2         \n\t"
231         "addl %%eax, %1         \n\t"
232         "addl %0, %%eax         \n\t"
233         "mull %4                \n\t"
234         "movl %%edx, %0         \n\t"
235         "movl %1, %%eax         \n\t"
236         "mull %4                \n\t"
237         "movl %%edx, %1         \n\t"
238         "movl %2, %%eax         \n\t"
239         "mull %4                \n\t"
240         "movl %%edx, %2         \n\t"
241         : "+b" (a), "+c" (b), "+D" (c)
242         : "g" (scale), "S" (ff_inverse[scale])
243         : "%eax", "%edx"
244     );
245 #else
246     /* #elif ARCH_ALPHA */
247     /* Divisions are extremely costly on Alpha; optimize the most
248        common case. But they are costly everywhere...
249      */
250     if (scale == 8) {
251         a = (a + (8 >> 1)) / 8;
252         b = (b + (8 >> 1)) / 8;
253         c = (c + (8 >> 1)) / 8;
254     } else {
255         a = FASTDIV((a + (scale >> 1)), scale);
256         b = FASTDIV((b + (scale >> 1)), scale);
257         c = FASTDIV((c + (scale >> 1)), scale);
258     }
259 #endif
260     /* XXX: WARNING: they did not choose the same test as MPEG4. This
261        is very important ! */
262     if(s->msmpeg4_version>3){
263         if(s->inter_intra_pred){
264             uint8_t *dest;
265             int wrap;
266
267             if(n==1){
268                 pred=a;
269                 *dir_ptr = 0;
270             }else if(n==2){
271                 pred=c;
272                 *dir_ptr = 1;
273             }else if(n==3){
274                 if (abs(a - b) < abs(b - c)) {
275                     pred = c;
276                     *dir_ptr = 1;
277                 } else {
278                     pred = a;
279                     *dir_ptr = 0;
280                 }
281             }else{
282                 if(n<4){
283                     wrap= s->linesize;
284                     dest= s->current_picture.f.data[0] + (((n >> 1) + 2*s->mb_y) * 8*  wrap ) + ((n & 1) + 2*s->mb_x) * 8;
285                 }else{
286                     wrap= s->uvlinesize;
287                     dest= s->current_picture.f.data[n - 3] + (s->mb_y * 8 * wrap) + s->mb_x * 8;
288                 }
289                 if(s->mb_x==0) a= (1024 + (scale>>1))/scale;
290                 else           a= get_dc(dest-8, wrap, scale*8);
291                 if(s->mb_y==0) c= (1024 + (scale>>1))/scale;
292                 else           c= get_dc(dest-8*wrap, wrap, scale*8);
293
294                 if (s->h263_aic_dir==0) {
295                     pred= a;
296                     *dir_ptr = 0;
297                 }else if (s->h263_aic_dir==1) {
298                     if(n==0){
299                         pred= c;
300                         *dir_ptr = 1;
301                     }else{
302                         pred= a;
303                         *dir_ptr = 0;
304                     }
305                 }else if (s->h263_aic_dir==2) {
306                     if(n==0){
307                         pred= a;
308                         *dir_ptr = 0;
309                     }else{
310                         pred= c;
311                         *dir_ptr = 1;
312                     }
313                 } else {
314                     pred= c;
315                     *dir_ptr = 1;
316                 }
317             }
318         }else{
319             if (abs(a - b) < abs(b - c)) {
320                 pred = c;
321                 *dir_ptr = 1;
322             } else {
323                 pred = a;
324                 *dir_ptr = 0;
325             }
326         }
327     }else{
328         if (abs(a - b) <= abs(b - c)) {
329             pred = c;
330             *dir_ptr = 1;
331         } else {
332             pred = a;
333             *dir_ptr = 0;
334         }
335     }
336
337     /* update predictor */
338     *dc_val_ptr = &dc_val[0];
339     return pred;
340 }
341