]> git.sesse.net Git - ffmpeg/blob - libavcodec/msmpeg4.c
bink: Factorize bink put_pixel
[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 "idctdsp.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 "mpegvideodata.h"
39 #include "vc1data.h"
40
41 /*
42  * You can also call this codec : MPEG4 with a twist !
43  *
44  * TODO:
45  *        - (encoding) select best mv table (two choices)
46  *        - (encoding) select best vlc/dc table
47  */
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->idsp.idct_permutation, &s->intra_scantable,   ff_wmv1_scantable[1]);
141         ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_wmv1_scantable[2]);
142         ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_wmv1_scantable[3]);
143         ff_init_scantable(s->idsp.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     /* Divisions are costly everywhere; optimize the most common case. */
247     if (scale == 8) {
248         a = (a + (8 >> 1)) / 8;
249         b = (b + (8 >> 1)) / 8;
250         c = (c + (8 >> 1)) / 8;
251     } else {
252         a = FASTDIV((a + (scale >> 1)), scale);
253         b = FASTDIV((b + (scale >> 1)), scale);
254         c = FASTDIV((c + (scale >> 1)), scale);
255     }
256 #endif
257     /* XXX: WARNING: they did not choose the same test as MPEG4. This
258        is very important ! */
259     if(s->msmpeg4_version>3){
260         if(s->inter_intra_pred){
261             uint8_t *dest;
262             int wrap;
263
264             if(n==1){
265                 pred=a;
266                 *dir_ptr = 0;
267             }else if(n==2){
268                 pred=c;
269                 *dir_ptr = 1;
270             }else if(n==3){
271                 if (abs(a - b) < abs(b - c)) {
272                     pred = c;
273                     *dir_ptr = 1;
274                 } else {
275                     pred = a;
276                     *dir_ptr = 0;
277                 }
278             }else{
279                 if(n<4){
280                     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{
283                     wrap= s->uvlinesize;
284                     dest= s->current_picture.f->data[n - 3] + (s->mb_y * 8 * wrap) + s->mb_x * 8;
285                 }
286                 if(s->mb_x==0) a= (1024 + (scale>>1))/scale;
287                 else           a= get_dc(dest-8, wrap, scale*8);
288                 if(s->mb_y==0) c= (1024 + (scale>>1))/scale;
289                 else           c= get_dc(dest-8*wrap, wrap, scale*8);
290
291                 if (s->h263_aic_dir==0) {
292                     pred= a;
293                     *dir_ptr = 0;
294                 }else if (s->h263_aic_dir==1) {
295                     if(n==0){
296                         pred= c;
297                         *dir_ptr = 1;
298                     }else{
299                         pred= a;
300                         *dir_ptr = 0;
301                     }
302                 }else if (s->h263_aic_dir==2) {
303                     if(n==0){
304                         pred= a;
305                         *dir_ptr = 0;
306                     }else{
307                         pred= c;
308                         *dir_ptr = 1;
309                     }
310                 } else {
311                     pred= c;
312                     *dir_ptr = 1;
313                 }
314             }
315         }else{
316             if (abs(a - b) < abs(b - c)) {
317                 pred = c;
318                 *dir_ptr = 1;
319             } else {
320                 pred = a;
321                 *dir_ptr = 0;
322             }
323         }
324     }else{
325         if (abs(a - b) <= abs(b - c)) {
326             pred = c;
327             *dir_ptr = 1;
328         } else {
329             pred = a;
330             *dir_ptr = 0;
331         }
332     }
333
334     /* update predictor */
335     *dc_val_ptr = &dc_val[0];
336     return pred;
337 }
338