FFmpeg
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 "libavutil/thread.h"
31 
32 #include "avcodec.h"
33 #include "idctdsp.h"
34 #include "mpegvideo.h"
35 #include "msmpeg4.h"
36 #include "libavutil/x86/asm.h"
37 #include "h263.h"
38 #include "mpeg4video.h"
39 #include "msmpeg4data.h"
40 #include "mpegvideodata.h"
41 #include "vc1data.h"
42 #include "libavutil/imgutils.h"
43 
44 /*
45  * You can also call this codec: MPEG-4 with a twist!
46  *
47  * TODO:
48  * - (encoding) select best mv table (two choices)
49  * - (encoding) select best vlc/dc table
50  */
51 
52 /* This table is practically identical to the one from H.263
53  * except that it is inverted. */
55 {
56  for (int level = -256; level < 256; level++) {
57  int uni_code, uni_len;
58  int size, v, l;
59  /* find number of bits */
60  size = 0;
61  v = abs(level);
62  while (v) {
63  v >>= 1;
64  size++;
65  }
66 
67  if (level < 0)
68  l = (-level) ^ ((1 << size) - 1);
69  else
70  l = level;
71 
72  /* luminance H.263 */
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
76 
77  if (size > 0) {
78  uni_code <<= size; uni_code |= l;
79  uni_len += size;
80  if (size > 8) {
81  uni_code <<= 1; uni_code |= 1;
82  uni_len++;
83  }
84  }
85  ff_v2_dc_lum_table[level + 256][0] = uni_code;
86  ff_v2_dc_lum_table[level + 256][1] = uni_len;
87 
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
92 
93  if (size > 0) {
94  uni_code <<= size; uni_code |= l;
95  uni_len +=size;
96  if (size > 8) {
97  uni_code <<= 1; uni_code |= 1;
98  uni_len++;
99  }
100  }
101  ff_v2_dc_chroma_table[level + 256][0] = uni_code;
102  ff_v2_dc_chroma_table[level + 256][1] = uni_len;
103  }
104 }
105 
107 {
108  static uint8_t rl_table_store[NB_RL_TABLES][2][2 * MAX_RUN + MAX_LEVEL + 3];
109 
110  for (int i = 0; i < NB_RL_TABLES; i++)
111  ff_rl_init(&ff_rl_table[i], rl_table_store[i]);
112 
114 }
115 
117 {
118  static AVOnce init_static_once = AV_ONCE_INIT;
119 
120  switch(s->msmpeg4_version){
121  case 1:
122  case 2:
123  s->y_dc_scale_table=
124  s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
125  break;
126  case 3:
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;
130  } else{
131  s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
132  s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
133  }
134  break;
135  case 4:
136  case 5:
137  s->y_dc_scale_table= ff_wmv1_y_dc_scale_table;
138  s->c_dc_scale_table= ff_wmv1_c_dc_scale_table;
139  break;
140 #if CONFIG_VC1_DECODER
141  case 6:
142  s->y_dc_scale_table= ff_wmv3_dc_scale_table;
143  s->c_dc_scale_table= ff_wmv3_dc_scale_table;
144  break;
145 #endif
146 
147  }
148 
149 
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]);
155  }
156  //Note the default tables are set in common_init in mpegvideo.c
157 
158  ff_thread_once(&init_static_once, msmpeg4_common_init_static);
159 }
160 
161 /* predict coded block */
162 int ff_msmpeg4_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
163 {
164  int xy, wrap, pred, a, b, c;
165 
166  xy = s->block_index[n];
167  wrap = s->b8_stride;
168 
169  /* B C
170  * A X
171  */
172  a = s->coded_block[xy - 1 ];
173  b = s->coded_block[xy - 1 - wrap];
174  c = s->coded_block[xy - wrap];
175 
176  if (b == c) {
177  pred = a;
178  } else {
179  pred = c;
180  }
181 
182  /* store value */
183  *coded_block_ptr = &s->coded_block[xy];
184 
185  return pred;
186 }
187 
188 static int get_dc(uint8_t *src, int stride, int scale, int block_size)
189 {
190  int y;
191  int sum=0;
192  for(y=0; y<block_size; y++){
193  int x;
194  for(x=0; x<block_size; x++){
195  sum+=src[x + y*stride];
196  }
197  }
198  return FASTDIV((sum + (scale>>1)), scale);
199 }
200 
201 /* dir = 0: left, dir = 1: top prediction */
203  int16_t **dc_val_ptr, int *dir_ptr)
204 {
205  int a, b, c, wrap, pred, scale;
206  int16_t *dc_val;
207 
208  /* find prediction */
209  if (n < 4) {
210  scale = s->y_dc_scale;
211  } else {
212  scale = s->c_dc_scale;
213  }
214 
215  wrap = s->block_wrap[n];
216  dc_val= s->dc_val[0] + s->block_index[n];
217 
218  /* B C
219  * A X
220  */
221  a = dc_val[ - 1];
222  b = dc_val[ - 1 - wrap];
223  c = dc_val[ - wrap];
224 
225  if(s->first_slice_line && (n&2)==0 && s->msmpeg4_version<4){
226  b=c=1024;
227  }
228 
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
234  __asm__ volatile(
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"
240  "imull %4 \n\t"
241  "movl %%edx, %0 \n\t"
242  "movl %1, %%eax \n\t"
243  "imull %4 \n\t"
244  "movl %%edx, %1 \n\t"
245  "movl %2, %%eax \n\t"
246  "imull %4 \n\t"
247  "movl %%edx, %2 \n\t"
248  : "+b" (a), "+c" (b), "+D" (c)
249  : "g" (scale), "S" (ff_inverse[scale])
250  : "%eax", "%edx"
251  );
252 #else
253  /* Divisions are costly everywhere; optimize the most common case. */
254  if (scale == 8) {
255  a = (a + (8 >> 1)) / 8;
256  b = (b + (8 >> 1)) / 8;
257  c = (c + (8 >> 1)) / 8;
258  } else {
259  a = FASTDIV((a + (scale >> 1)), scale);
260  b = FASTDIV((b + (scale >> 1)), scale);
261  c = FASTDIV((c + (scale >> 1)), scale);
262  }
263 #endif
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){
268  uint8_t *dest;
269  int wrap;
270 
271  if(n==1){
272  pred=a;
273  *dir_ptr = 0;
274  }else if(n==2){
275  pred=c;
276  *dir_ptr = 1;
277  }else if(n==3){
278  if (abs(a - b) < abs(b - c)) {
279  pred = c;
280  *dir_ptr = 1;
281  } else {
282  pred = a;
283  *dir_ptr = 0;
284  }
285  }else{
286  int bs = 8 >> s->avctx->lowres;
287  if(n<4){
288  wrap= s->linesize;
289  dest= s->current_picture.f->data[0] + (((n >> 1) + 2*s->mb_y) * bs* wrap ) + ((n & 1) + 2*s->mb_x) * bs;
290  }else{
291  wrap= s->uvlinesize;
292  dest= s->current_picture.f->data[n - 3] + (s->mb_y * bs * wrap) + s->mb_x * bs;
293  }
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);
298 
299  if (s->h263_aic_dir==0) {
300  pred= a;
301  *dir_ptr = 0;
302  }else if (s->h263_aic_dir==1) {
303  if(n==0){
304  pred= c;
305  *dir_ptr = 1;
306  }else{
307  pred= a;
308  *dir_ptr = 0;
309  }
310  }else if (s->h263_aic_dir==2) {
311  if(n==0){
312  pred= a;
313  *dir_ptr = 0;
314  }else{
315  pred= c;
316  *dir_ptr = 1;
317  }
318  } else {
319  pred= c;
320  *dir_ptr = 1;
321  }
322  }
323  }else{
324  if (abs(a - b) < abs(b - c)) {
325  pred = c;
326  *dir_ptr = 1;
327  } else {
328  pred = a;
329  *dir_ptr = 0;
330  }
331  }
332  }else{
333  if (abs(a - b) <= abs(b - c)) {
334  pred = c;
335  *dir_ptr = 1;
336  } else {
337  pred = a;
338  *dir_ptr = 0;
339  }
340  }
341 
342  /* update predictor */
343  *dc_val_ptr = &dc_val[0];
344  return pred;
345 }
346 
stride
int stride
Definition: mace.c:144
ff_init_scantable
av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: idctdsp.c:29
level
uint8_t level
Definition: svq3.c:204
thread.h
ff_mpeg1_dc_scale_table
const uint8_t ff_mpeg1_dc_scale_table[128]
Definition: mpegvideodata.c:33
ff_wmv1_scantable
const uint8_t ff_wmv1_scantable[WMV1_SCANTABLE_COUNT][64]
Definition: msmpeg4data.c:1806
MAX_RUN
#define MAX_RUN
Definition: rl.h:35
ff_msmpeg4_common_init
av_cold void ff_msmpeg4_common_init(MpegEncContext *s)
Definition: msmpeg4.c:116
b
#define b
Definition: input.c:40
mpegvideo.h
ff_rl_table
RLTable ff_rl_table[NB_RL_TABLES]
Definition: msmpeg4data.c:600
ff_inverse
const uint32_t ff_inverse[257]
Definition: mathtables.c:27
ff_mpeg4_DCtab_chrom
const uint8_t ff_mpeg4_DCtab_chrom[13][2]
Definition: mpeg4data.h:41
wrap
#define wrap(func)
Definition: neontest.h:65
scale
static av_always_inline float scale(float x, float s)
Definition: vf_v360.c:1388
NB_RL_TABLES
#define NB_RL_TABLES
Definition: msmpeg4data.h:59
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:175
ff_wmv3_dc_scale_table
const uint8_t ff_wmv3_dc_scale_table[32]
Definition: vc1data.c:648
av_cold
#define av_cold
Definition: attributes.h:90
msmpeg4data.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
ff_rl_init
av_cold void ff_rl_init(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
Initialize index_run, max_level and max_run from n, last, table_vlc, table_run and table_level.
Definition: rl.c:27
ff_mpeg4_DCtab_lum
const uint8_t ff_mpeg4_DCtab_lum[13][2]
Definition: mpeg4data.h:35
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:173
ff_v2_dc_lum_table
uint32_t ff_v2_dc_lum_table[512][2]
Definition: msmpeg4data.c:34
src
#define src
Definition: vp8dsp.c:255
get_dc
static int get_dc(uint8_t *src, int stride, int scale, int block_size)
Definition: msmpeg4.c:188
ff_v2_dc_chroma_table
uint32_t ff_v2_dc_chroma_table[512][2]
Definition: msmpeg4data.c:35
abs
#define abs(x)
Definition: cuda_runtime.h:35
FASTDIV
#define FASTDIV(a, b)
Definition: mathops.h:202
AVOnce
#define AVOnce
Definition: thread.h:172
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
MAX_LEVEL
#define MAX_LEVEL
Definition: rl.h:36
ff_mpeg4_y_dc_scale_table
const uint8_t ff_mpeg4_y_dc_scale_table[32]
Definition: mpeg4data.h:357
size
int size
Definition: twinvq_data.h:10344
asm.h
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
vc1data.h
ff_msmpeg4_pred_dc
int ff_msmpeg4_pred_dc(MpegEncContext *s, int n, int16_t **dc_val_ptr, int *dir_ptr)
Definition: msmpeg4.c:202
mpegvideodata.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
msmpeg4_common_init_static
static av_cold void msmpeg4_common_init_static(void)
Definition: msmpeg4.c:106
idctdsp.h
avcodec.h
msmpeg4.h
__asm__
__asm__(".macro parse_r var r\n\t" "\\var = -1\n\t" _IFC_REG(0) _IFC_REG(1) _IFC_REG(2) _IFC_REG(3) _IFC_REG(4) _IFC_REG(5) _IFC_REG(6) _IFC_REG(7) _IFC_REG(8) _IFC_REG(9) _IFC_REG(10) _IFC_REG(11) _IFC_REG(12) _IFC_REG(13) _IFC_REG(14) _IFC_REG(15) _IFC_REG(16) _IFC_REG(17) _IFC_REG(18) _IFC_REG(19) _IFC_REG(20) _IFC_REG(21) _IFC_REG(22) _IFC_REG(23) _IFC_REG(24) _IFC_REG(25) _IFC_REG(26) _IFC_REG(27) _IFC_REG(28) _IFC_REG(29) _IFC_REG(30) _IFC_REG(31) ".iflt \\var\n\t" ".error \"Unable to parse register name \\r\"\n\t" ".endif\n\t" ".endm")
pred
static const float pred[4]
Definition: siprdata.h:259
ff_old_ff_y_dc_scale_table
const uint8_t ff_old_ff_y_dc_scale_table[32]
Definition: msmpeg4data.c:1801
mpeg4video.h
ff_mpeg4_c_dc_scale_table
const uint8_t ff_mpeg4_c_dc_scale_table[32]
Definition: mpeg4data.h:361
ff_wmv1_y_dc_scale_table
const uint8_t ff_wmv1_y_dc_scale_table[32]
Definition: msmpeg4data.c:1792
init_h263_dc_for_msmpeg4
static av_cold void init_h263_dc_for_msmpeg4(void)
Definition: msmpeg4.c:54
ff_wmv1_c_dc_scale_table
const uint8_t ff_wmv1_c_dc_scale_table[32]
Definition: msmpeg4data.c:1796
imgutils.h
MpegEncContext
MpegEncContext.
Definition: mpegvideo.h:71
ff_msmpeg4_coded_block_pred
int ff_msmpeg4_coded_block_pred(MpegEncContext *s, int n, uint8_t **coded_block_ptr)
Definition: msmpeg4.c:162
h263.h