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 FFmpeg.
10 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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
30 #include "libavutil/thread.h"
34 #include "mpegvideo.h"
36 #include "libavutil/x86/asm.h"
38 #include "mpeg4video.h"
39 #include "msmpeg4data.h"
40 #include "mpegvideodata.h"
42 #include "libavutil/imgutils.h"
45 * You can also call this codec: MPEG-4 with a twist!
48 * - (encoding) select best mv table (two choices)
49 * - (encoding) select best vlc/dc table
52 /* This table is practically identical to the one from H.263
53 * except that it is inverted. */
54 static av_cold void init_h263_dc_for_msmpeg4(void)
56 for (int level = -256; level < 256; level++) {
57 int uni_code, uni_len;
59 /* find number of bits */
68 l = (-level) ^ ((1 << size) - 1);
73 uni_code = ff_mpeg4_DCtab_lum[size][0];
74 uni_len = ff_mpeg4_DCtab_lum[size][1];
75 uni_code ^= (1 << uni_len) - 1; //M$ does not like compatibility
78 uni_code <<= size; uni_code |= l;
81 uni_code <<= 1; uni_code |= 1;
85 ff_v2_dc_lum_table[level + 256][0] = uni_code;
86 ff_v2_dc_lum_table[level + 256][1] = uni_len;
88 /* chrominance H.263 */
89 uni_code = ff_mpeg4_DCtab_chrom[size][0];
90 uni_len = ff_mpeg4_DCtab_chrom[size][1];
91 uni_code ^= (1 << uni_len) - 1; //M$ does not like compatibility
94 uni_code <<= size; uni_code |= l;
97 uni_code <<= 1; uni_code |= 1;
101 ff_v2_dc_chroma_table[level + 256][0] = uni_code;
102 ff_v2_dc_chroma_table[level + 256][1] = uni_len;
106 static av_cold void msmpeg4_common_init_static(void)
108 static uint8_t rl_table_store[NB_RL_TABLES][2][2 * MAX_RUN + MAX_LEVEL + 3];
110 for (int i = 0; i < NB_RL_TABLES; i++)
111 ff_rl_init(&ff_rl_table[i], rl_table_store[i]);
113 init_h263_dc_for_msmpeg4();
116 av_cold void ff_msmpeg4_common_init(MpegEncContext *s)
118 static AVOnce init_static_once = AV_ONCE_INIT;
120 switch(s->msmpeg4_version){
124 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
127 if(s->workaround_bugs){
128 s->y_dc_scale_table= ff_old_ff_y_dc_scale_table;
129 s->c_dc_scale_table= ff_wmv1_c_dc_scale_table;
131 s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
132 s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
137 s->y_dc_scale_table= ff_wmv1_y_dc_scale_table;
138 s->c_dc_scale_table= ff_wmv1_c_dc_scale_table;
140 #if CONFIG_VC1_DECODER
142 s->y_dc_scale_table= ff_wmv3_dc_scale_table;
143 s->c_dc_scale_table= ff_wmv3_dc_scale_table;
150 if(s->msmpeg4_version>=4){
151 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_wmv1_scantable[1]);
152 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_wmv1_scantable[2]);
153 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_wmv1_scantable[3]);
154 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_wmv1_scantable[0]);
156 //Note the default tables are set in common_init in mpegvideo.c
158 ff_thread_once(&init_static_once, msmpeg4_common_init_static);
161 /* predict coded block */
162 int ff_msmpeg4_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
164 int xy, wrap, pred, a, b, c;
166 xy = s->block_index[n];
172 a = s->coded_block[xy - 1 ];
173 b = s->coded_block[xy - 1 - wrap];
174 c = s->coded_block[xy - wrap];
183 *coded_block_ptr = &s->coded_block[xy];
188 static int get_dc(uint8_t *src, int stride, int scale, int block_size)
192 for(y=0; y<block_size; y++){
194 for(x=0; x<block_size; x++){
195 sum+=src[x + y*stride];
198 return FASTDIV((sum + (scale>>1)), scale);
201 /* dir = 0: left, dir = 1: top prediction */
202 int ff_msmpeg4_pred_dc(MpegEncContext *s, int n,
203 int16_t **dc_val_ptr, int *dir_ptr)
205 int a, b, c, wrap, pred, scale;
208 /* find prediction */
210 scale = s->y_dc_scale;
212 scale = s->c_dc_scale;
215 wrap = s->block_wrap[n];
216 dc_val= s->dc_val[0] + s->block_index[n];
222 b = dc_val[ - 1 - wrap];
225 if(s->first_slice_line && (n&2)==0 && s->msmpeg4_version<4){
229 /* XXX: the following solution consumes divisions, but it does not
230 necessitate to modify mpegvideo.c. The problem comes from the
231 fact they decided to store the quantized DC (which would lead
232 to problems if Q could vary !) */
233 #if ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE
235 "movl %3, %%eax \n\t"
236 "shrl $1, %%eax \n\t"
237 "addl %%eax, %2 \n\t"
238 "addl %%eax, %1 \n\t"
239 "addl %0, %%eax \n\t"
241 "movl %%edx, %0 \n\t"
242 "movl %1, %%eax \n\t"
244 "movl %%edx, %1 \n\t"
245 "movl %2, %%eax \n\t"
247 "movl %%edx, %2 \n\t"
248 : "+b" (a), "+c" (b), "+D" (c)
249 : "g" (scale), "S" (ff_inverse[scale])
253 /* Divisions are costly everywhere; optimize the most common case. */
255 a = (a + (8 >> 1)) / 8;
256 b = (b + (8 >> 1)) / 8;
257 c = (c + (8 >> 1)) / 8;
259 a = FASTDIV((a + (scale >> 1)), scale);
260 b = FASTDIV((b + (scale >> 1)), scale);
261 c = FASTDIV((c + (scale >> 1)), scale);
264 /* XXX: WARNING: they did not choose the same test as MPEG-4. This
265 is very important ! */
266 if(s->msmpeg4_version>3){
267 if(s->inter_intra_pred){
278 if (abs(a - b) < abs(b - c)) {
286 int bs = 8 >> s->avctx->lowres;
289 dest= s->current_picture.f->data[0] + (((n >> 1) + 2*s->mb_y) * bs* wrap ) + ((n & 1) + 2*s->mb_x) * bs;
292 dest= s->current_picture.f->data[n - 3] + (s->mb_y * bs * wrap) + s->mb_x * bs;
294 if(s->mb_x==0) a= (1024 + (scale>>1))/scale;
295 else a= get_dc(dest-bs, wrap, scale*8>>(2*s->avctx->lowres), bs);
296 if(s->mb_y==0) c= (1024 + (scale>>1))/scale;
297 else c= get_dc(dest-bs*wrap, wrap, scale*8>>(2*s->avctx->lowres), bs);
299 if (s->h263_aic_dir==0) {
302 }else if (s->h263_aic_dir==1) {
310 }else if (s->h263_aic_dir==2) {
324 if (abs(a - b) < abs(b - c)) {
333 if (abs(a - b) <= abs(b - c)) {
342 /* update predictor */
343 *dc_val_ptr = &dc_val[0];