Libav
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 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 
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: MPEG-4 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 H.263
50  * except that it is inverted. */
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 H.263 */
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 H.263 */
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 
106 {
107  static int initialized=0;
108 
109  switch(s->msmpeg4_version){
110  case 1:
111  case 2:
112  s->y_dc_scale_table=
114  break;
115  case 3:
116  if(s->workaround_bugs){
119  } else{
122  }
123  break;
124  case 4:
125  case 5:
128  break;
129 #if CONFIG_VC1_DECODER
130  case 6:
133  break;
134 #endif
135 
136  }
137 
138 
139  if(s->msmpeg4_version>=4){
144  }
145  //Note the default tables are set in common_init in mpegvideo.c
146 
147  if(!initialized){
148  initialized=1;
149 
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 */
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 MPEG-4. 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 
int inter_intra_pred
Definition: mpegvideo.h:427
IDCTDSPContext idsp
Definition: mpegvideo.h:221
ScanTable intra_v_scantable
Definition: mpegvideo.h:88
int size
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:183
uint32_t ff_v2_dc_lum_table[512][2]
Definition: msmpeg4data.c:34
uint32_t ff_v2_dc_chroma_table[512][2]
Definition: msmpeg4data.c:35
int msmpeg4_version
0=not msmpeg4, 1=mp41, 2=mp42, 3=mp43/divx3 4=wmv1/7 5=wmv2/8
Definition: mpegvideo.h:421
mpegvideo header.
int stride
Definition: mace.c:144
av_cold void ff_msmpeg4_common_init(MpegEncContext *s)
Definition: msmpeg4.c:105
int block_wrap[6]
Definition: mpegvideo.h:288
VC-1 tables.
const uint8_t ff_mpeg1_dc_scale_table[128]
Definition: mpegvideodata.c:27
const uint32_t ff_inverse[257]
Definition: mathtables.c:25
uint8_t
#define av_cold
Definition: attributes.h:66
#define b
Definition: input.c:52
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:175
const uint8_t ff_mpeg4_DCtab_chrom[13][2]
Definition: mpeg4data.h:41
MSMPEG4 data tables.
const uint8_t ff_wmv1_c_dc_scale_table[32]
Definition: msmpeg4data.c:1799
#define src
Definition: vp8dsp.c:254
int16_t * dc_val[3]
used for MPEG-4 DC prediction, all 3 arrays must be continuous
Definition: mpegvideo.h:182
#define wrap(func)
Definition: neontest.h:62
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:1809
int ff_msmpeg4_pred_dc(MpegEncContext *s, int n, int16_t **dc_val_ptr, int *dir_ptr)
Definition: msmpeg4.c:195
const uint8_t ff_mpeg4_y_dc_scale_table[32]
Definition: mpeg4data.h:359
uint8_t idct_permutation[64]
IDCT input permutation.
Definition: idctdsp.h:94
int block_index[6]
index to current MB in block based arrays with edges
Definition: mpegvideo.h:287
static const float pred[4]
Definition: siprdata.h:259
int first_slice_line
used in MPEG-4 too to handle resync markers
Definition: mpegvideo.h:419
Libavcodec external API header.
ptrdiff_t linesize
line size, in bytes, may be different from width
Definition: mpegvideo.h:129
#define FASTDIV(a, b)
Definition: mathops.h:190
ScanTable intra_scantable
Definition: mpegvideo.h:86
uint8_t * coded_block
used for coded block pattern prediction (msmpeg4v3, wmv1)
Definition: mpegvideo.h:187
ScanTable intra_h_scantable
Definition: mpegvideo.h:87
struct AVFrame * f
Definition: mpegpicture.h:46
ptrdiff_t uvlinesize
line size, for chroma in bytes, may be different from width
Definition: mpegvideo.h:130
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
const uint8_t * c_dc_scale_table
qscale -> c_dc_scale table
Definition: mpegvideo.h:184
uint8_t level
Definition: svq3.c:204
int b8_stride
2*mb_width+1 used for some 8x8 block arrays to allow simple addressing
Definition: mpegvideo.h:126
MpegEncContext.
Definition: mpegvideo.h:76
const uint8_t ff_wmv1_y_dc_scale_table[32]
Definition: msmpeg4data.c:1795
av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: idctdsp.c:28
const uint8_t ff_old_ff_y_dc_scale_table[32]
Definition: msmpeg4data.c:1804
int workaround_bugs
workaround bugs in encoders which cannot be detected automatically
Definition: mpegvideo.h:114
ScanTable inter_scantable
if inter == intra then intra should be used to reduce the cache usage
Definition: mpegvideo.h:85
int ff_msmpeg4_coded_block_pred(MpegEncContext *s, int n, uint8_t **coded_block_ptr)
Definition: msmpeg4.c:155
int h263_aic_dir
AIC direction: 0 = left, 1 = top.
Definition: mpegvideo.h:363
static av_cold void init_h263_dc_for_msmpeg4(void)
Definition: msmpeg4.c:51
static int get_dc(uint8_t *src, int stride, int scale)
Definition: msmpeg4.c:181