mpegvideo: Move tables to a separate file
[libav.git] / libavcodec / msmpeg4.c
CommitLineData
de6d9b64 1/*
124e2884 2 * MSMPEG4 backend for encoder and decoder
406792e7 3 * Copyright (c) 2001 Fabrice Bellard
8f2ab833 4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
de6d9b64 5 *
7b94177e
DB
6 * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
7 *
2912e87a 8 * This file is part of Libav.
b78e7197 9 *
2912e87a 10 * Libav is free software; you can redistribute it and/or
ff4ec49e
FB
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
b78e7197 13 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 14 *
2912e87a 15 * Libav is distributed in the hope that it will be useful,
de6d9b64 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
de6d9b64 19 *
ff4ec49e 20 * You should have received a copy of the GNU Lesser General Public
2912e87a 21 * License along with Libav; if not, write to the Free Software
5509bffa 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 23 */
983e3246
MN
24
25/**
ba87f080 26 * @file
124e2884 27 * MSMPEG4 backend for encoder and decoder
983e3246
MN
28 */
29
6000abfa 30#include "avcodec.h"
e3fcb143 31#include "idctdsp.h"
de6d9b64 32#include "mpegvideo.h"
cc6de104 33#include "msmpeg4.h"
c318626c 34#include "libavutil/x86/asm.h"
ca334dd1
MN
35#include "h263.h"
36#include "mpeg4video.h"
2f4b476e 37#include "msmpeg4data.h"
378a0008 38#include "mpegvideodata.h"
bd7a647c 39#include "vc1data.h"
92ba5ffb 40
de6d9b64 41/*
115329f1 42 * You can also call this codec : MPEG4 with a twist !
de6d9b64 43 *
115329f1 44 * TODO:
de6d9b64 45 * - (encoding) select best mv table (two choices)
115329f1 46 * - (encoding) select best vlc/dc table
de6d9b64 47 */
de6d9b64 48
8380edd9
DB
49/* This table is practically identical to the one from h263
50 * except that it is inverted. */
51static 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 */
05b858b0
MN
71 uni_code= ff_mpeg4_DCtab_lum[size][0];
72 uni_len = ff_mpeg4_DCtab_lum[size][1];
8380edd9
DB
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 }
2f4b476e
DB
83 ff_v2_dc_lum_table[level + 256][0] = uni_code;
84 ff_v2_dc_lum_table[level + 256][1] = uni_len;
8380edd9
DB
85
86 /* chrominance h263 */
05b858b0
MN
87 uni_code= ff_mpeg4_DCtab_chrom[size][0];
88 uni_len = ff_mpeg4_DCtab_chrom[size][1];
8380edd9
DB
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 }
2f4b476e
DB
99 ff_v2_dc_chroma_table[level + 256][0] = uni_code;
100 ff_v2_dc_chroma_table[level + 256][1] = uni_len;
8380edd9
DB
101
102 }
103}
104
2f4b476e 105av_cold void ff_msmpeg4_common_init(MpegEncContext *s)
f5957f3f 106{
5e534865 107 static int initialized=0;
115329f1 108
f5957f3f
MN
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){
1fec0550
MS
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;
f5957f3f
MN
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:
1457ab52 125 case 5:
1fec0550
MS
126 s->y_dc_scale_table= ff_wmv1_y_dc_scale_table;
127 s->c_dc_scale_table= ff_wmv1_c_dc_scale_table;
f5957f3f 128 break;
9be6f0d2 129#if CONFIG_VC1_DECODER
0d33db8a 130 case 6:
5f2c159c
MS
131 s->y_dc_scale_table= ff_wmv3_dc_scale_table;
132 s->c_dc_scale_table= ff_wmv3_dc_scale_table;
0d33db8a 133 break;
8b975b7c 134#endif
0d33db8a 135
f5957f3f
MN
136 }
137
115329f1 138
1457ab52 139 if(s->msmpeg4_version>=4){
e3fcb143
DB
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]);
f5957f3f 144 }
2ad1516a 145 //Note the default tables are set in common_init in mpegvideo.c
115329f1 146
5e534865
DB
147 if(!initialized){
148 initialized=1;
f5957f3f
MN
149
150 init_h263_dc_for_msmpeg4();
f5957f3f
MN
151 }
152}
153
de6d9b64 154/* predict coded block */
85f601ee 155int ff_msmpeg4_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
de6d9b64 156{
dbbe8999 157 int xy, wrap, pred, a, b, c;
de6d9b64 158
dbbe8999 159 xy = s->block_index[n];
137c8468 160 wrap = s->b8_stride;
de6d9b64
FB
161
162 /* B C
115329f1 163 * A X
de6d9b64 164 */
dbbe8999
MN
165 a = s->coded_block[xy - 1 ];
166 b = s->coded_block[xy - 1 - wrap];
167 c = s->coded_block[xy - wrap];
115329f1 168
de6d9b64
FB
169 if (b == c) {
170 pred = a;
171 } else {
172 pred = c;
173 }
115329f1 174
de6d9b64 175 /* store value */
dbbe8999 176 *coded_block_ptr = &s->coded_block[xy];
de6d9b64
FB
177
178 return pred;
179}
180
de0f2f4c
MN
181static 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 }
d4961b35 191 return FASTDIV((sum + (scale>>1)), scale);
de0f2f4c
MN
192}
193
de6d9b64 194/* dir = 0: left, dir = 1: top prediction */
2f4b476e
DB
195int ff_msmpeg4_pred_dc(MpegEncContext *s, int n,
196 int16_t **dc_val_ptr, int *dir_ptr)
de6d9b64 197{
dbbe8999 198 int a, b, c, wrap, pred, scale;
b86216de 199 int16_t *dc_val;
de6d9b64
FB
200
201 /* find prediction */
202 if (n < 4) {
bb270c08 203 scale = s->y_dc_scale;
de6d9b64 204 } else {
bb270c08 205 scale = s->c_dc_scale;
de6d9b64 206 }
115329f1 207
dbbe8999
MN
208 wrap = s->block_wrap[n];
209 dc_val= s->dc_val[0] + s->block_index[n];
de6d9b64
FB
210
211 /* B C
115329f1 212 * A X
de6d9b64 213 */
dbbe8999
MN
214 a = dc_val[ - 1];
215 b = dc_val[ - 1 - wrap];
216 c = dc_val[ - wrap];
115329f1 217
28269849 218 if(s->first_slice_line && (n&2)==0 && s->msmpeg4_version<4){
4d2858de
MN
219 b=c=1024;
220 }
de6d9b64
FB
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 !) */
f2d702e1 226#if ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE
be449fca 227 __asm__ volatile(
bb270c08
DB
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)
36cd3069 242 : "g" (scale), "S" (ff_inverse[scale])
bb270c08 243 : "%eax", "%edx"
6f903d8e 244 );
320680d4 245#else
c7f7bfc9 246 /* Divisions are costly everywhere; optimize the most common case. */
1e98dffb 247 if (scale == 8) {
bb270c08
DB
248 a = (a + (8 >> 1)) / 8;
249 b = (b + (8 >> 1)) / 8;
250 c = (c + (8 >> 1)) / 8;
1e98dffb 251 } else {
bb270c08
DB
252 a = FASTDIV((a + (scale >> 1)), scale);
253 b = FASTDIV((b + (scale >> 1)), scale);
254 c = FASTDIV((c + (scale >> 1)), scale);
1e98dffb 255 }
6f903d8e 256#endif
de6d9b64
FB
257 /* XXX: WARNING: they did not choose the same test as MPEG4. This
258 is very important ! */
bd5e1c72 259 if(s->msmpeg4_version>3){
de0f2f4c
MN
260 if(s->inter_intra_pred){
261 uint8_t *dest;
262 int wrap;
115329f1 263
de0f2f4c
MN
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;
f6774f90 281 dest= s->current_picture.f->data[0] + (((n >> 1) + 2*s->mb_y) * 8* wrap ) + ((n & 1) + 2*s->mb_x) * 8;
de0f2f4c 282 }else{
0fd90455 283 wrap= s->uvlinesize;
f6774f90 284 dest= s->current_picture.f->data[n - 3] + (s->mb_y * 8 * wrap) + s->mb_x * 8;
de0f2f4c
MN
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);
115329f1 290
de0f2f4c
MN
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 }
bd5e1c72
MN
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 }
de6d9b64
FB
332 }
333
334 /* update predictor */
dbbe8999 335 *dc_val_ptr = &dc_val[0];
de6d9b64
FB
336 return pred;
337}
338