FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mpegvideoenc_template.c
Go to the documentation of this file.
1 /*
2  * MPEG video MMX templates
3  *
4  * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include <stdint.h>
24 
25 #include "libavutil/internal.h"
26 #include "libavutil/x86/asm.h"
27 #include "libavcodec/mpegvideo.h"
28 
29 #undef MMREG_WIDTH
30 #undef MM
31 #undef MOVQ
32 #undef SPREADW
33 #undef PMAXW
34 #undef PMAX
35 #undef SAVE_SIGN
36 #undef RESTORE_SIGN
37 
38 #if COMPILE_TEMPLATE_SSE2
39 #define MMREG_WIDTH "16"
40 #define MM "%%xmm"
41 #define MOVQ "movdqa"
42 #define SPREADW(a) \
43  "pshuflw $0, "a", "a" \n\t"\
44  "punpcklwd "a", "a" \n\t"
45 #define PMAXW(a,b) "pmaxsw "a", "b" \n\t"
46 #define PMAX(a,b) \
47  "movhlps "a", "b" \n\t"\
48  PMAXW(b, a)\
49  "pshuflw $0x0E, "a", "b" \n\t"\
50  PMAXW(b, a)\
51  "pshuflw $0x01, "a", "b" \n\t"\
52  PMAXW(b, a)
53 #else
54 #define MMREG_WIDTH "8"
55 #define MM "%%mm"
56 #define MOVQ "movq"
57 #if COMPILE_TEMPLATE_MMXEXT
58 #define SPREADW(a) "pshufw $0, "a", "a" \n\t"
59 #define PMAXW(a,b) "pmaxsw "a", "b" \n\t"
60 #define PMAX(a,b) \
61  "pshufw $0x0E, "a", "b" \n\t"\
62  PMAXW(b, a)\
63  "pshufw $0x01, "a", "b" \n\t"\
64  PMAXW(b, a)
65 #else
66 #define SPREADW(a) \
67  "punpcklwd "a", "a" \n\t"\
68  "punpcklwd "a", "a" \n\t"
69 #define PMAXW(a,b) \
70  "psubusw "a", "b" \n\t"\
71  "paddw "a", "b" \n\t"
72 #define PMAX(a,b) \
73  "movq "a", "b" \n\t"\
74  "psrlq $32, "a" \n\t"\
75  PMAXW(b, a)\
76  "movq "a", "b" \n\t"\
77  "psrlq $16, "a" \n\t"\
78  PMAXW(b, a)
79 
80 #endif
81 #endif
82 
83 #if COMPILE_TEMPLATE_SSSE3
84 #define SAVE_SIGN(a,b) \
85  "movdqa "b", "a" \n\t"\
86  "pabsw "b", "b" \n\t"
87 #define RESTORE_SIGN(a,b) \
88  "psignw "a", "b" \n\t"
89 #else
90 #define SAVE_SIGN(a,b) \
91  "pxor "a", "a" \n\t"\
92  "pcmpgtw "b", "a" \n\t" /* block[i] <= 0 ? 0xFF : 0x00 */\
93  "pxor "a", "b" \n\t"\
94  "psubw "a", "b" \n\t" /* ABS(block[i]) */
95 #define RESTORE_SIGN(a,b) \
96  "pxor "a", "b" \n\t"\
97  "psubw "a", "b" \n\t" // out=((ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16)*sign(block[i])
98 #endif
99 
100 static int RENAME(dct_quantize)(MpegEncContext *s,
101  int16_t *block, int n,
102  int qscale, int *overflow)
103 {
104  x86_reg last_non_zero_p1;
105  int level=0, q; //=0 is because gcc says uninitialized ...
106  const uint16_t *qmat, *bias;
107  LOCAL_ALIGNED_16(int16_t, temp_block, [64]);
108 
109  av_assert2((7&(int)(&temp_block[0])) == 0); //did gcc align it correctly?
110 
111  //s->fdct (block);
112  RENAME_FDCT(ff_fdct)(block); // cannot be anything else ...
113 
114  if(s->dct_error_sum)
115  s->denoise_dct(s, block);
116 
117  if (s->mb_intra) {
118  int dummy;
119  if (n < 4){
120  q = s->y_dc_scale;
121  bias = s->q_intra_matrix16[qscale][1];
122  qmat = s->q_intra_matrix16[qscale][0];
123  }else{
124  q = s->c_dc_scale;
125  bias = s->q_chroma_intra_matrix16[qscale][1];
126  qmat = s->q_chroma_intra_matrix16[qscale][0];
127  }
128  /* note: block[0] is assumed to be positive */
129  if (!s->h263_aic) {
130  __asm__ volatile (
131  "mul %%ecx \n\t"
132  : "=d" (level), "=a"(dummy)
133  : "a" ((block[0]>>2) + q), "c" (ff_inverse[q<<1])
134  );
135  } else
136  /* For AIC we skip quant/dequant of INTRADC */
137  level = (block[0] + 4)>>3;
138 
139  block[0]=0; //avoid fake overflow
140 // temp_block[0] = (block[0] + (q >> 1)) / q;
141  last_non_zero_p1 = 1;
142  } else {
143  last_non_zero_p1 = 0;
144  bias = s->q_inter_matrix16[qscale][1];
145  qmat = s->q_inter_matrix16[qscale][0];
146  }
147 
148  if((s->out_format == FMT_H263 || s->out_format == FMT_H261) && s->mpeg_quant==0){
149 
150  __asm__ volatile(
151  "movd %%"REG_a", "MM"3 \n\t" // last_non_zero_p1
152  SPREADW(MM"3")
153  "pxor "MM"7, "MM"7 \n\t" // 0
154  "pxor "MM"4, "MM"4 \n\t" // 0
155  MOVQ" (%2), "MM"5 \n\t" // qmat[0]
156  "pxor "MM"6, "MM"6 \n\t"
157  "psubw (%3), "MM"6 \n\t" // -bias[0]
158  "mov $-128, %%"REG_a" \n\t"
159  ".p2align 4 \n\t"
160  "1: \n\t"
161  MOVQ" (%1, %%"REG_a"), "MM"0 \n\t" // block[i]
162  SAVE_SIGN(MM"1", MM"0") // ABS(block[i])
163  "psubusw "MM"6, "MM"0 \n\t" // ABS(block[i]) + bias[0]
164  "pmulhw "MM"5, "MM"0 \n\t" // (ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16
165  "por "MM"0, "MM"4 \n\t"
166  RESTORE_SIGN(MM"1", MM"0") // out=((ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16)*sign(block[i])
167  MOVQ" "MM"0, (%5, %%"REG_a") \n\t"
168  "pcmpeqw "MM"7, "MM"0 \n\t" // out==0 ? 0xFF : 0x00
169  MOVQ" (%4, %%"REG_a"), "MM"1 \n\t"
170  MOVQ" "MM"7, (%1, %%"REG_a") \n\t" // 0
171  "pandn "MM"1, "MM"0 \n\t"
172  PMAXW(MM"0", MM"3")
173  "add $"MMREG_WIDTH", %%"REG_a" \n\t"
174  " js 1b \n\t"
175  PMAX(MM"3", MM"0")
176  "movd "MM"3, %%"REG_a" \n\t"
177  "movzbl %%al, %%eax \n\t" // last_non_zero_p1
178  : "+a" (last_non_zero_p1)
179  : "r" (block+64), "r" (qmat), "r" (bias),
180  "r" (inv_zigzag_direct16 + 64), "r" (temp_block + 64)
181  XMM_CLOBBERS_ONLY("%xmm0", "%xmm1", "%xmm2", "%xmm3",
182  "%xmm4", "%xmm5", "%xmm6", "%xmm7")
183  );
184  }else{ // FMT_H263
185  __asm__ volatile(
186  "movd %%"REG_a", "MM"3 \n\t" // last_non_zero_p1
187  SPREADW(MM"3")
188  "pxor "MM"7, "MM"7 \n\t" // 0
189  "pxor "MM"4, "MM"4 \n\t" // 0
190  "mov $-128, %%"REG_a" \n\t"
191  ".p2align 4 \n\t"
192  "1: \n\t"
193  MOVQ" (%1, %%"REG_a"), "MM"0 \n\t" // block[i]
194  SAVE_SIGN(MM"1", MM"0") // ABS(block[i])
195  MOVQ" (%3, %%"REG_a"), "MM"6 \n\t" // bias[0]
196  "paddusw "MM"6, "MM"0 \n\t" // ABS(block[i]) + bias[0]
197  MOVQ" (%2, %%"REG_a"), "MM"5 \n\t" // qmat[i]
198  "pmulhw "MM"5, "MM"0 \n\t" // (ABS(block[i])*qmat[0] + bias[0]*qmat[0])>>16
199  "por "MM"0, "MM"4 \n\t"
200  RESTORE_SIGN(MM"1", MM"0") // out=((ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16)*sign(block[i])
201  MOVQ" "MM"0, (%5, %%"REG_a") \n\t"
202  "pcmpeqw "MM"7, "MM"0 \n\t" // out==0 ? 0xFF : 0x00
203  MOVQ" (%4, %%"REG_a"), "MM"1 \n\t"
204  MOVQ" "MM"7, (%1, %%"REG_a") \n\t" // 0
205  "pandn "MM"1, "MM"0 \n\t"
206  PMAXW(MM"0", MM"3")
207  "add $"MMREG_WIDTH", %%"REG_a" \n\t"
208  " js 1b \n\t"
209  PMAX(MM"3", MM"0")
210  "movd "MM"3, %%"REG_a" \n\t"
211  "movzbl %%al, %%eax \n\t" // last_non_zero_p1
212  : "+a" (last_non_zero_p1)
213  : "r" (block+64), "r" (qmat+64), "r" (bias+64),
214  "r" (inv_zigzag_direct16 + 64), "r" (temp_block + 64)
215  XMM_CLOBBERS_ONLY("%xmm0", "%xmm1", "%xmm2", "%xmm3",
216  "%xmm4", "%xmm5", "%xmm6", "%xmm7")
217  );
218  }
219  __asm__ volatile(
220  "movd %1, "MM"1 \n\t" // max_qcoeff
221  SPREADW(MM"1")
222  "psubusw "MM"1, "MM"4 \n\t"
223  "packuswb "MM"4, "MM"4 \n\t"
224 #if COMPILE_TEMPLATE_SSE2
225  "packsswb "MM"4, "MM"4 \n\t"
226 #endif
227  "movd "MM"4, %0 \n\t" // *overflow
228  : "=g" (*overflow)
229  : "g" (s->max_qcoeff)
230  );
231 
232  if(s->mb_intra) block[0]= level;
233  else block[0]= temp_block[0];
234 
235  if (s->idsp.idct_permutation_type == FF_SIMPLE_IDCT_PERM) {
236  if(last_non_zero_p1 <= 1) goto end;
237  block[0x08] = temp_block[0x01]; block[0x10] = temp_block[0x08];
238  block[0x20] = temp_block[0x10];
239  if(last_non_zero_p1 <= 4) goto end;
240  block[0x18] = temp_block[0x09]; block[0x04] = temp_block[0x02];
241  block[0x09] = temp_block[0x03];
242  if(last_non_zero_p1 <= 7) goto end;
243  block[0x14] = temp_block[0x0A]; block[0x28] = temp_block[0x11];
244  block[0x12] = temp_block[0x18]; block[0x02] = temp_block[0x20];
245  if(last_non_zero_p1 <= 11) goto end;
246  block[0x1A] = temp_block[0x19]; block[0x24] = temp_block[0x12];
247  block[0x19] = temp_block[0x0B]; block[0x01] = temp_block[0x04];
248  block[0x0C] = temp_block[0x05];
249  if(last_non_zero_p1 <= 16) goto end;
250  block[0x11] = temp_block[0x0C]; block[0x29] = temp_block[0x13];
251  block[0x16] = temp_block[0x1A]; block[0x0A] = temp_block[0x21];
252  block[0x30] = temp_block[0x28]; block[0x22] = temp_block[0x30];
253  block[0x38] = temp_block[0x29]; block[0x06] = temp_block[0x22];
254  if(last_non_zero_p1 <= 24) goto end;
255  block[0x1B] = temp_block[0x1B]; block[0x21] = temp_block[0x14];
256  block[0x1C] = temp_block[0x0D]; block[0x05] = temp_block[0x06];
257  block[0x0D] = temp_block[0x07]; block[0x15] = temp_block[0x0E];
258  block[0x2C] = temp_block[0x15]; block[0x13] = temp_block[0x1C];
259  if(last_non_zero_p1 <= 32) goto end;
260  block[0x0B] = temp_block[0x23]; block[0x34] = temp_block[0x2A];
261  block[0x2A] = temp_block[0x31]; block[0x32] = temp_block[0x38];
262  block[0x3A] = temp_block[0x39]; block[0x26] = temp_block[0x32];
263  block[0x39] = temp_block[0x2B]; block[0x03] = temp_block[0x24];
264  if(last_non_zero_p1 <= 40) goto end;
265  block[0x1E] = temp_block[0x1D]; block[0x25] = temp_block[0x16];
266  block[0x1D] = temp_block[0x0F]; block[0x2D] = temp_block[0x17];
267  block[0x17] = temp_block[0x1E]; block[0x0E] = temp_block[0x25];
268  block[0x31] = temp_block[0x2C]; block[0x2B] = temp_block[0x33];
269  if(last_non_zero_p1 <= 48) goto end;
270  block[0x36] = temp_block[0x3A]; block[0x3B] = temp_block[0x3B];
271  block[0x23] = temp_block[0x34]; block[0x3C] = temp_block[0x2D];
272  block[0x07] = temp_block[0x26]; block[0x1F] = temp_block[0x1F];
273  block[0x0F] = temp_block[0x27]; block[0x35] = temp_block[0x2E];
274  if(last_non_zero_p1 <= 56) goto end;
275  block[0x2E] = temp_block[0x35]; block[0x33] = temp_block[0x3C];
276  block[0x3E] = temp_block[0x3D]; block[0x27] = temp_block[0x36];
277  block[0x3D] = temp_block[0x2F]; block[0x2F] = temp_block[0x37];
278  block[0x37] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
279  }else if(s->idsp.idct_permutation_type == FF_LIBMPEG2_IDCT_PERM){
280  if(last_non_zero_p1 <= 1) goto end;
281  block[0x04] = temp_block[0x01];
282  block[0x08] = temp_block[0x08]; block[0x10] = temp_block[0x10];
283  if(last_non_zero_p1 <= 4) goto end;
284  block[0x0C] = temp_block[0x09]; block[0x01] = temp_block[0x02];
285  block[0x05] = temp_block[0x03];
286  if(last_non_zero_p1 <= 7) goto end;
287  block[0x09] = temp_block[0x0A]; block[0x14] = temp_block[0x11];
288  block[0x18] = temp_block[0x18]; block[0x20] = temp_block[0x20];
289  if(last_non_zero_p1 <= 11) goto end;
290  block[0x1C] = temp_block[0x19];
291  block[0x11] = temp_block[0x12]; block[0x0D] = temp_block[0x0B];
292  block[0x02] = temp_block[0x04]; block[0x06] = temp_block[0x05];
293  if(last_non_zero_p1 <= 16) goto end;
294  block[0x0A] = temp_block[0x0C]; block[0x15] = temp_block[0x13];
295  block[0x19] = temp_block[0x1A]; block[0x24] = temp_block[0x21];
296  block[0x28] = temp_block[0x28]; block[0x30] = temp_block[0x30];
297  block[0x2C] = temp_block[0x29]; block[0x21] = temp_block[0x22];
298  if(last_non_zero_p1 <= 24) goto end;
299  block[0x1D] = temp_block[0x1B]; block[0x12] = temp_block[0x14];
300  block[0x0E] = temp_block[0x0D]; block[0x03] = temp_block[0x06];
301  block[0x07] = temp_block[0x07]; block[0x0B] = temp_block[0x0E];
302  block[0x16] = temp_block[0x15]; block[0x1A] = temp_block[0x1C];
303  if(last_non_zero_p1 <= 32) goto end;
304  block[0x25] = temp_block[0x23]; block[0x29] = temp_block[0x2A];
305  block[0x34] = temp_block[0x31]; block[0x38] = temp_block[0x38];
306  block[0x3C] = temp_block[0x39]; block[0x31] = temp_block[0x32];
307  block[0x2D] = temp_block[0x2B]; block[0x22] = temp_block[0x24];
308  if(last_non_zero_p1 <= 40) goto end;
309  block[0x1E] = temp_block[0x1D]; block[0x13] = temp_block[0x16];
310  block[0x0F] = temp_block[0x0F]; block[0x17] = temp_block[0x17];
311  block[0x1B] = temp_block[0x1E]; block[0x26] = temp_block[0x25];
312  block[0x2A] = temp_block[0x2C]; block[0x35] = temp_block[0x33];
313  if(last_non_zero_p1 <= 48) goto end;
314  block[0x39] = temp_block[0x3A]; block[0x3D] = temp_block[0x3B];
315  block[0x32] = temp_block[0x34]; block[0x2E] = temp_block[0x2D];
316  block[0x23] = temp_block[0x26]; block[0x1F] = temp_block[0x1F];
317  block[0x27] = temp_block[0x27]; block[0x2B] = temp_block[0x2E];
318  if(last_non_zero_p1 <= 56) goto end;
319  block[0x36] = temp_block[0x35]; block[0x3A] = temp_block[0x3C];
320  block[0x3E] = temp_block[0x3D]; block[0x33] = temp_block[0x36];
321  block[0x2F] = temp_block[0x2F]; block[0x37] = temp_block[0x37];
322  block[0x3B] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
323  }else{
324  if(last_non_zero_p1 <= 1) goto end;
325  block[0x01] = temp_block[0x01];
326  block[0x08] = temp_block[0x08]; block[0x10] = temp_block[0x10];
327  if(last_non_zero_p1 <= 4) goto end;
328  block[0x09] = temp_block[0x09]; block[0x02] = temp_block[0x02];
329  block[0x03] = temp_block[0x03];
330  if(last_non_zero_p1 <= 7) goto end;
331  block[0x0A] = temp_block[0x0A]; block[0x11] = temp_block[0x11];
332  block[0x18] = temp_block[0x18]; block[0x20] = temp_block[0x20];
333  if(last_non_zero_p1 <= 11) goto end;
334  block[0x19] = temp_block[0x19];
335  block[0x12] = temp_block[0x12]; block[0x0B] = temp_block[0x0B];
336  block[0x04] = temp_block[0x04]; block[0x05] = temp_block[0x05];
337  if(last_non_zero_p1 <= 16) goto end;
338  block[0x0C] = temp_block[0x0C]; block[0x13] = temp_block[0x13];
339  block[0x1A] = temp_block[0x1A]; block[0x21] = temp_block[0x21];
340  block[0x28] = temp_block[0x28]; block[0x30] = temp_block[0x30];
341  block[0x29] = temp_block[0x29]; block[0x22] = temp_block[0x22];
342  if(last_non_zero_p1 <= 24) goto end;
343  block[0x1B] = temp_block[0x1B]; block[0x14] = temp_block[0x14];
344  block[0x0D] = temp_block[0x0D]; block[0x06] = temp_block[0x06];
345  block[0x07] = temp_block[0x07]; block[0x0E] = temp_block[0x0E];
346  block[0x15] = temp_block[0x15]; block[0x1C] = temp_block[0x1C];
347  if(last_non_zero_p1 <= 32) goto end;
348  block[0x23] = temp_block[0x23]; block[0x2A] = temp_block[0x2A];
349  block[0x31] = temp_block[0x31]; block[0x38] = temp_block[0x38];
350  block[0x39] = temp_block[0x39]; block[0x32] = temp_block[0x32];
351  block[0x2B] = temp_block[0x2B]; block[0x24] = temp_block[0x24];
352  if(last_non_zero_p1 <= 40) goto end;
353  block[0x1D] = temp_block[0x1D]; block[0x16] = temp_block[0x16];
354  block[0x0F] = temp_block[0x0F]; block[0x17] = temp_block[0x17];
355  block[0x1E] = temp_block[0x1E]; block[0x25] = temp_block[0x25];
356  block[0x2C] = temp_block[0x2C]; block[0x33] = temp_block[0x33];
357  if(last_non_zero_p1 <= 48) goto end;
358  block[0x3A] = temp_block[0x3A]; block[0x3B] = temp_block[0x3B];
359  block[0x34] = temp_block[0x34]; block[0x2D] = temp_block[0x2D];
360  block[0x26] = temp_block[0x26]; block[0x1F] = temp_block[0x1F];
361  block[0x27] = temp_block[0x27]; block[0x2E] = temp_block[0x2E];
362  if(last_non_zero_p1 <= 56) goto end;
363  block[0x35] = temp_block[0x35]; block[0x3C] = temp_block[0x3C];
364  block[0x3D] = temp_block[0x3D]; block[0x36] = temp_block[0x36];
365  block[0x2F] = temp_block[0x2F]; block[0x37] = temp_block[0x37];
366  block[0x3E] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
367  }
368  end:
369  return last_non_zero_p1 - 1;
370 }