msmpeg4.c
Go to the documentation of this file.
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 FFmpeg.
9  *
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.
14  *
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.
19  *
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
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 #include "libavutil/imgutils.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 //#define DEBUG
49 
50 /* This table is practically identical to the one from h263
51  * except that it is inverted. */
53 {
54  int level, uni_code, uni_len;
55 
56  if(ff_v2_dc_chroma_table[255 + 256][1])
57  return;
58 
59  for(level=-256; level<256; level++){
60  int size, v, l;
61  /* find number of bits */
62  size = 0;
63  v = abs(level);
64  while (v) {
65  v >>= 1;
66  size++;
67  }
68 
69  if (level < 0)
70  l= (-level) ^ ((1 << size) - 1);
71  else
72  l= level;
73 
74  /* luminance h263 */
75  uni_code= ff_mpeg4_DCtab_lum[size][0];
76  uni_len = ff_mpeg4_DCtab_lum[size][1];
77  uni_code ^= (1<<uni_len)-1; //M$ does not like compatibility
78 
79  if (size > 0) {
80  uni_code<<=size; uni_code|=l;
81  uni_len+=size;
82  if (size > 8){
83  uni_code<<=1; uni_code|=1;
84  uni_len++;
85  }
86  }
87  ff_v2_dc_lum_table[level + 256][0] = uni_code;
88  ff_v2_dc_lum_table[level + 256][1] = uni_len;
89 
90  /* chrominance h263 */
91  uni_code= ff_mpeg4_DCtab_chrom[size][0];
92  uni_len = ff_mpeg4_DCtab_chrom[size][1];
93  uni_code ^= (1<<uni_len)-1; //M$ does not like compatibility
94 
95  if (size > 0) {
96  uni_code<<=size; uni_code|=l;
97  uni_len+=size;
98  if (size > 8){
99  uni_code<<=1; uni_code|=1;
100  uni_len++;
101  }
102  }
103  ff_v2_dc_chroma_table[level + 256][0] = uni_code;
104  ff_v2_dc_chroma_table[level + 256][1] = uni_len;
105 
106  }
107 }
108 
110 {
111  switch(s->msmpeg4_version){
112  case 1:
113  case 2:
114  s->y_dc_scale_table=
116  break;
117  case 3:
118  if(s->workaround_bugs){
121  } else{
124  }
125  break;
126  case 4:
127  case 5:
130  break;
131 #if CONFIG_VC1_DECODER
132  case 6:
135  break;
136 #endif
137 
138  }
139 
140 
141  if(s->msmpeg4_version>=4){
146  }
147  //Note the default tables are set in common_init in mpegvideo.c
148 
150 }
151 
152 /* predict coded block */
153 int ff_msmpeg4_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
154 {
155  int xy, wrap, pred, a, b, c;
156 
157  xy = s->block_index[n];
158  wrap = s->b8_stride;
159 
160  /* B C
161  * A X
162  */
163  a = s->coded_block[xy - 1 ];
164  b = s->coded_block[xy - 1 - wrap];
165  c = s->coded_block[xy - wrap];
166 
167  if (b == c) {
168  pred = a;
169  } else {
170  pred = c;
171  }
172 
173  /* store value */
174  *coded_block_ptr = &s->coded_block[xy];
175 
176  return pred;
177 }
178 
179 static int get_dc(uint8_t *src, int stride, int scale)
180 {
181  int y;
182  int sum=0;
183  for(y=0; y<8; y++){
184  int x;
185  for(x=0; x<8; x++){
186  sum+=src[x + y*stride];
187  }
188  }
189  return FASTDIV((sum + (scale>>1)), scale);
190 }
191 
192 /* dir = 0: left, dir = 1: top prediction */
194  int16_t **dc_val_ptr, int *dir_ptr)
195 {
196  int a, b, c, wrap, pred, scale;
197  int16_t *dc_val;
198 
199  /* find prediction */
200  if (n < 4) {
201  scale = s->y_dc_scale;
202  } else {
203  scale = s->c_dc_scale;
204  }
205 
206  wrap = s->block_wrap[n];
207  dc_val= s->dc_val[0] + s->block_index[n];
208 
209  /* B C
210  * A X
211  */
212  a = dc_val[ - 1];
213  b = dc_val[ - 1 - wrap];
214  c = dc_val[ - wrap];
215 
216  if(s->first_slice_line && (n&2)==0 && s->msmpeg4_version<4){
217  b=c=1024;
218  }
219 
220  /* XXX: the following solution consumes divisions, but it does not
221  necessitate to modify mpegvideo.c. The problem comes from the
222  fact they decided to store the quantized DC (which would lead
223  to problems if Q could vary !) */
224 #if ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE
225  __asm__ volatile(
226  "movl %3, %%eax \n\t"
227  "shrl $1, %%eax \n\t"
228  "addl %%eax, %2 \n\t"
229  "addl %%eax, %1 \n\t"
230  "addl %0, %%eax \n\t"
231  "imull %4 \n\t"
232  "movl %%edx, %0 \n\t"
233  "movl %1, %%eax \n\t"
234  "imull %4 \n\t"
235  "movl %%edx, %1 \n\t"
236  "movl %2, %%eax \n\t"
237  "imull %4 \n\t"
238  "movl %%edx, %2 \n\t"
239  : "+b" (a), "+c" (b), "+D" (c)
240  : "g" (scale), "S" (ff_inverse[scale])
241  : "%eax", "%edx"
242  );
243 #else
244  /* #elif ARCH_ALPHA */
245  /* Divisions are extremely costly on Alpha; optimize the most
246  common case. But they are costly everywhere...
247  */
248  if (scale == 8) {
249  a = (a + (8 >> 1)) / 8;
250  b = (b + (8 >> 1)) / 8;
251  c = (c + (8 >> 1)) / 8;
252  } else {
253  a = FASTDIV((a + (scale >> 1)), scale);
254  b = FASTDIV((b + (scale >> 1)), scale);
255  c = FASTDIV((c + (scale >> 1)), scale);
256  }
257 #endif
258  /* XXX: WARNING: they did not choose the same test as MPEG4. This
259  is very important ! */
260  if(s->msmpeg4_version>3){
261  if(s->inter_intra_pred){
262  uint8_t *dest;
263  int wrap;
264 
265  if(n==1){
266  pred=a;
267  *dir_ptr = 0;
268  }else if(n==2){
269  pred=c;
270  *dir_ptr = 1;
271  }else if(n==3){
272  if (abs(a - b) < abs(b - c)) {
273  pred = c;
274  *dir_ptr = 1;
275  } else {
276  pred = a;
277  *dir_ptr = 0;
278  }
279  }else{
280  if(n<4){
281  wrap= s->linesize;
282  dest= s->current_picture.f.data[0] + (((n >> 1) + 2*s->mb_y) * 8* wrap ) + ((n & 1) + 2*s->mb_x) * 8;
283  }else{
284  wrap= s->uvlinesize;
285  dest= s->current_picture.f.data[n - 3] + (s->mb_y * 8 * wrap) + s->mb_x * 8;
286  }
287  if(s->mb_x==0) a= (1024 + (scale>>1))/scale;
288  else a= get_dc(dest-8, wrap, scale*8);
289  if(s->mb_y==0) c= (1024 + (scale>>1))/scale;
290  else c= get_dc(dest-8*wrap, wrap, scale*8);
291 
292  if (s->h263_aic_dir==0) {
293  pred= a;
294  *dir_ptr = 0;
295  }else if (s->h263_aic_dir==1) {
296  if(n==0){
297  pred= c;
298  *dir_ptr = 1;
299  }else{
300  pred= a;
301  *dir_ptr = 0;
302  }
303  }else if (s->h263_aic_dir==2) {
304  if(n==0){
305  pred= a;
306  *dir_ptr = 0;
307  }else{
308  pred= c;
309  *dir_ptr = 1;
310  }
311  } else {
312  pred= c;
313  *dir_ptr = 1;
314  }
315  }
316  }else{
317  if (abs(a - b) < abs(b - c)) {
318  pred = c;
319  *dir_ptr = 1;
320  } else {
321  pred = a;
322  *dir_ptr = 0;
323  }
324  }
325  }else{
326  if (abs(a - b) <= abs(b - c)) {
327  pred = c;
328  *dir_ptr = 1;
329  } else {
330  pred = a;
331  *dir_ptr = 0;
332  }
333  }
334 
335  /* update predictor */
336  *dc_val_ptr = &dc_val[0];
337  return pred;
338 }
339 
int inter_intra_pred
Definition: mpegvideo.h:645
float v
const char * s
Definition: avisynth_c.h:668
ScanTable intra_v_scantable
Definition: mpegvideo.h:298
const uint8_t ff_mpeg4_c_dc_scale_table[32]
Definition: mpeg4data.h:363
const uint8_t * y_dc_scale_table
qscale -> y_dc_scale table
Definition: mpegvideo.h:351
uint32_t ff_v2_dc_lum_table[512][2]
Definition: msmpeg4data.c:32
misc image utilities
uint32_t ff_v2_dc_chroma_table[512][2]
Definition: msmpeg4data.c:33
int msmpeg4_version
0=not msmpeg4, 1=mp41, 2=mp42, 3=mp43/divx3 4=wmv1/7 5=wmv2/8
Definition: mpegvideo.h:639
#define wrap(func)
Definition: w64xmmtest.h:70
mpegvideo header.
int stride
Definition: mace.c:144
av_cold void ff_msmpeg4_common_init(MpegEncContext *s)
Definition: msmpeg4.c:109
int block_wrap[6]
Definition: mpegvideo.h:466
VC-1 tables.
const uint32_t ff_inverse[257]
Definition: mathtables.c:25
uint8_t
#define av_cold
Definition: attributes.h:78
#define b
Definition: input.c:42
const uint8_t ff_wmv3_dc_scale_table[32]
Definition: vc1data.c:648
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:343
const uint8_t ff_mpeg4_DCtab_chrom[13][2]
Definition: mpeg4data.h:41
MSMPEG4 data tables.
uint8_t idct_permutation[64]
idct input permutation.
Definition: dsputil.h:249
const uint8_t ff_wmv1_c_dc_scale_table[32]
Definition: msmpeg4data.c:1821
Discrete Time axis x
int16_t * dc_val[3]
used for mpeg4 DC prediction, all 3 arrays must be continuous
Definition: mpegvideo.h:350
external API header
int size
const uint8_t ff_mpeg4_DCtab_lum[13][2]
Definition: mpeg4data.h:35
const uint8_t ff_wmv1_scantable[WMV1_SCANTABLE_COUNT][64]
Definition: msmpeg4data.c:1831
int ff_msmpeg4_pred_dc(MpegEncContext *s, int n, int16_t **dc_val_ptr, int *dir_ptr)
Definition: msmpeg4.c:193
const uint8_t ff_mpeg4_y_dc_scale_table[32]
Definition: mpeg4data.h:359
FIXME Range Coding of cr are level
Definition: snow.txt:367
int block_index[6]
index to current MB in block based arrays with edges
Definition: mpegvideo.h:465
static const float pred[4]
Definition: siprdata.h:259
int first_slice_line
used in mpeg4 too to handle resync markers
Definition: mpegvideo.h:637
dest
Definition: start.py:60
AVS_Value src
Definition: avisynth_c.h:523
#define FASTDIV(a, b)
Definition: mathops.h:195
ScanTable intra_scantable
Definition: mpegvideo.h:296
uint8_t * coded_block
used for coded block pattern prediction (msmpeg4v3, wmv1)
Definition: mpegvideo.h:355
const uint8_t ff_mpeg1_dc_scale_table[128]
Definition: mpegvideo.c:72
ScanTable intra_h_scantable
Definition: mpegvideo.h:297
DSPContext dsp
pointers for accelerated dsp functions
Definition: mpegvideo.h:391
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
const uint8_t * c_dc_scale_table
qscale -> c_dc_scale table
Definition: mpegvideo.h:352
int b8_stride
2*mb_width+1 used for some 8x8 block arrays to allow simple addressing
Definition: mpegvideo.h:279
MpegEncContext.
Definition: mpegvideo.h:241
static double c[64]
Same thing on a dB scale
function y
Definition: D.m:1
DSP utils.
const uint8_t ff_wmv1_y_dc_scale_table[32]
Definition: msmpeg4data.c:1817
void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: dsputil.c:110
int linesize
line size, in bytes, may be different from width
Definition: mpegvideo.h:283
struct AVFrame f
Definition: mpegvideo.h:98
const uint8_t ff_old_ff_y_dc_scale_table[32]
Definition: msmpeg4data.c:1826
int workaround_bugs
workaround bugs in encoders which cannot be detected automatically
Definition: mpegvideo.h:266
ScanTable inter_scantable
if inter == intra then intra should be used to reduce tha cache usage
Definition: mpegvideo.h:295
int ff_msmpeg4_coded_block_pred(MpegEncContext *s, int n, uint8_t **coded_block_ptr)
Definition: msmpeg4.c:153
int h263_aic_dir
AIC direction: 0 = left, 1 = top.
Definition: mpegvideo.h:547
int uvlinesize
line size, for chroma in bytes, may be different from width
Definition: mpegvideo.h:284
static av_cold void init_h263_dc_for_msmpeg4(void)
Definition: msmpeg4.c:52
static int get_dc(uint8_t *src, int stride, int scale)
Definition: msmpeg4.c:179