2 * MSMPEG4 backend for encoder and decoder
3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
8 * This file is part of Libav.
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.
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.
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
27 * MSMPEG4 backend for encoder and decoder
32 #include "mpegvideo.h"
34 #include "libavutil/x86/asm.h"
36 #include "mpeg4video.h"
37 #include "msmpeg4data.h"
41 * You can also call this codec : MPEG4 with a twist !
44 * - (encoding) select best mv table (two choices)
45 * - (encoding) select best vlc/dc table
48 /* This table is practically identical to the one from h263
49 * except that it is inverted. */
50 static av_cold void init_h263_dc_for_msmpeg4(void)
52 int level, uni_code, uni_len;
54 for(level=-256; level<256; level++){
56 /* find number of bits */
65 l= (-level) ^ ((1 << size) - 1);
70 uni_code= ff_mpeg4_DCtab_lum[size][0];
71 uni_len = ff_mpeg4_DCtab_lum[size][1];
72 uni_code ^= (1<<uni_len)-1; //M$ does not like compatibility
75 uni_code<<=size; uni_code|=l;
78 uni_code<<=1; uni_code|=1;
82 ff_v2_dc_lum_table[level + 256][0] = uni_code;
83 ff_v2_dc_lum_table[level + 256][1] = uni_len;
85 /* chrominance h263 */
86 uni_code= ff_mpeg4_DCtab_chrom[size][0];
87 uni_len = ff_mpeg4_DCtab_chrom[size][1];
88 uni_code ^= (1<<uni_len)-1; //M$ does not like compatibility
91 uni_code<<=size; uni_code|=l;
94 uni_code<<=1; uni_code|=1;
98 ff_v2_dc_chroma_table[level + 256][0] = uni_code;
99 ff_v2_dc_chroma_table[level + 256][1] = uni_len;
104 av_cold void ff_msmpeg4_common_init(MpegEncContext *s)
106 static int initialized=0;
108 switch(s->msmpeg4_version){
112 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
115 if(s->workaround_bugs){
116 s->y_dc_scale_table= ff_old_ff_y_dc_scale_table;
117 s->c_dc_scale_table= ff_wmv1_c_dc_scale_table;
119 s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
120 s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
125 s->y_dc_scale_table= ff_wmv1_y_dc_scale_table;
126 s->c_dc_scale_table= ff_wmv1_c_dc_scale_table;
128 #if CONFIG_VC1_DECODER
130 s->y_dc_scale_table= ff_wmv3_dc_scale_table;
131 s->c_dc_scale_table= ff_wmv3_dc_scale_table;
138 if(s->msmpeg4_version>=4){
139 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_wmv1_scantable[1]);
140 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_wmv1_scantable[2]);
141 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_wmv1_scantable[3]);
142 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_wmv1_scantable[0]);
144 //Note the default tables are set in common_init in mpegvideo.c
149 init_h263_dc_for_msmpeg4();
153 /* predict coded block */
154 int ff_msmpeg4_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
156 int xy, wrap, pred, a, b, c;
158 xy = s->block_index[n];
164 a = s->coded_block[xy - 1 ];
165 b = s->coded_block[xy - 1 - wrap];
166 c = s->coded_block[xy - wrap];
175 *coded_block_ptr = &s->coded_block[xy];
180 static int get_dc(uint8_t *src, int stride, int scale)
187 sum+=src[x + y*stride];
190 return FASTDIV((sum + (scale>>1)), scale);
193 /* dir = 0: left, dir = 1: top prediction */
194 int ff_msmpeg4_pred_dc(MpegEncContext *s, int n,
195 int16_t **dc_val_ptr, int *dir_ptr)
197 int a, b, c, wrap, pred, scale;
200 /* find prediction */
202 scale = s->y_dc_scale;
204 scale = s->c_dc_scale;
207 wrap = s->block_wrap[n];
208 dc_val= s->dc_val[0] + s->block_index[n];
214 b = dc_val[ - 1 - wrap];
217 if(s->first_slice_line && (n&2)==0 && s->msmpeg4_version<4){
221 /* XXX: the following solution consumes divisions, but it does not
222 necessitate to modify mpegvideo.c. The problem comes from the
223 fact they decided to store the quantized DC (which would lead
224 to problems if Q could vary !) */
225 #if ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE
227 "movl %3, %%eax \n\t"
228 "shrl $1, %%eax \n\t"
229 "addl %%eax, %2 \n\t"
230 "addl %%eax, %1 \n\t"
231 "addl %0, %%eax \n\t"
233 "movl %%edx, %0 \n\t"
234 "movl %1, %%eax \n\t"
236 "movl %%edx, %1 \n\t"
237 "movl %2, %%eax \n\t"
239 "movl %%edx, %2 \n\t"
240 : "+b" (a), "+c" (b), "+D" (c)
241 : "g" (scale), "S" (ff_inverse[scale])
245 /* Divisions are costly everywhere; optimize the most common case. */
247 a = (a + (8 >> 1)) / 8;
248 b = (b + (8 >> 1)) / 8;
249 c = (c + (8 >> 1)) / 8;
251 a = FASTDIV((a + (scale >> 1)), scale);
252 b = FASTDIV((b + (scale >> 1)), scale);
253 c = FASTDIV((c + (scale >> 1)), scale);
256 /* XXX: WARNING: they did not choose the same test as MPEG4. This
257 is very important ! */
258 if(s->msmpeg4_version>3){
259 if(s->inter_intra_pred){
270 if (abs(a - b) < abs(b - c)) {
280 dest= s->current_picture.f->data[0] + (((n >> 1) + 2*s->mb_y) * 8* wrap ) + ((n & 1) + 2*s->mb_x) * 8;
283 dest= s->current_picture.f->data[n - 3] + (s->mb_y * 8 * wrap) + s->mb_x * 8;
285 if(s->mb_x==0) a= (1024 + (scale>>1))/scale;
286 else a= get_dc(dest-8, wrap, scale*8);
287 if(s->mb_y==0) c= (1024 + (scale>>1))/scale;
288 else c= get_dc(dest-8*wrap, wrap, scale*8);
290 if (s->h263_aic_dir==0) {
293 }else if (s->h263_aic_dir==1) {
301 }else if (s->h263_aic_dir==2) {
315 if (abs(a - b) < abs(b - c)) {
324 if (abs(a - b) <= abs(b - c)) {
333 /* update predictor */
334 *dc_val_ptr = &dc_val[0];