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/mpegutils.h"
28 #include "libavcodec/mpegvideo.h"
29 #include "fdct.h"
30 
31 #undef MMREG_WIDTH
32 #undef MM
33 #undef MOVQ
34 #undef SPREADW
35 #undef PMAXW
36 #undef PMAX
37 #undef SAVE_SIGN
38 #undef RESTORE_SIGN
39 
40 #if COMPILE_TEMPLATE_SSE2
41 #define MMREG_WIDTH "16"
42 #define MM "%%xmm"
43 #define MOVQ "movdqa"
44 #define SPREADW(a) \
45  "pshuflw $0, "a", "a" \n\t"\
46  "punpcklwd "a", "a" \n\t"
47 #define PMAXW(a,b) "pmaxsw "a", "b" \n\t"
48 #define PMAX(a,b) \
49  "movhlps "a", "b" \n\t"\
50  PMAXW(b, a)\
51  "pshuflw $0x0E, "a", "b" \n\t"\
52  PMAXW(b, a)\
53  "pshuflw $0x01, "a", "b" \n\t"\
54  PMAXW(b, a)
55 #else
56 #define MMREG_WIDTH "8"
57 #define MM "%%mm"
58 #define MOVQ "movq"
59 #if COMPILE_TEMPLATE_MMXEXT
60 #define SPREADW(a) "pshufw $0, "a", "a" \n\t"
61 #define PMAXW(a,b) "pmaxsw "a", "b" \n\t"
62 #define PMAX(a,b) \
63  "pshufw $0x0E, "a", "b" \n\t"\
64  PMAXW(b, a)\
65  "pshufw $0x01, "a", "b" \n\t"\
66  PMAXW(b, a)
67 #else
68 #define SPREADW(a) \
69  "punpcklwd "a", "a" \n\t"\
70  "punpcklwd "a", "a" \n\t"
71 #define PMAXW(a,b) \
72  "psubusw "a", "b" \n\t"\
73  "paddw "a", "b" \n\t"
74 #define PMAX(a,b) \
75  "movq "a", "b" \n\t"\
76  "psrlq $32, "a" \n\t"\
77  PMAXW(b, a)\
78  "movq "a", "b" \n\t"\
79  "psrlq $16, "a" \n\t"\
80  PMAXW(b, a)
81 
82 #endif
83 #endif
84 
85 #if COMPILE_TEMPLATE_SSSE3
86 #define SAVE_SIGN(a,b) \
87  "movdqa "b", "a" \n\t"\
88  "pabsw "b", "b" \n\t"
89 #define RESTORE_SIGN(a,b) \
90  "psignw "a", "b" \n\t"
91 #else
92 #define SAVE_SIGN(a,b) \
93  "pxor "a", "a" \n\t"\
94  "pcmpgtw "b", "a" \n\t" /* block[i] <= 0 ? 0xFF : 0x00 */\
95  "pxor "a", "b" \n\t"\
96  "psubw "a", "b" \n\t" /* ABS(block[i]) */
97 #define RESTORE_SIGN(a,b) \
98  "pxor "a", "b" \n\t"\
99  "psubw "a", "b" \n\t" // out=((ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16)*sign(block[i])
100 #endif
101 
102 static int RENAME(dct_quantize)(MpegEncContext *s,
103  int16_t *block, int n,
104  int qscale, int *overflow)
105 {
106  x86_reg last_non_zero_p1;
107  int level=0, q; //=0 is because gcc says uninitialized ...
108  const uint16_t *qmat, *bias;
109  LOCAL_ALIGNED_16(int16_t, temp_block, [64]);
110 
111  av_assert2((7&(int)(&temp_block[0])) == 0); //did gcc align it correctly?
112 
113  //s->fdct (block);
114  RENAME_FDCT(ff_fdct)(block); // cannot be anything else ...
115 
116  if(s->dct_error_sum)
117  s->denoise_dct(s, block);
118 
119  if (s->mb_intra) {
120  int dummy;
121  if (n < 4){
122  q = s->y_dc_scale;
123  bias = s->q_intra_matrix16[qscale][1];
124  qmat = s->q_intra_matrix16[qscale][0];
125  }else{
126  q = s->c_dc_scale;
127  bias = s->q_chroma_intra_matrix16[qscale][1];
128  qmat = s->q_chroma_intra_matrix16[qscale][0];
129  }
130  /* note: block[0] is assumed to be positive */
131  if (!s->h263_aic) {
132  __asm__ volatile (
133  "mul %%ecx \n\t"
134  : "=d" (level), "=a"(dummy)
135  : "a" ((block[0]>>2) + q), "c" (ff_inverse[q<<1])
136  );
137  } else
138  /* For AIC we skip quant/dequant of INTRADC */
139  level = (block[0] + 4)>>3;
140 
141  block[0]=0; //avoid fake overflow
142 // temp_block[0] = (block[0] + (q >> 1)) / q;
143  last_non_zero_p1 = 1;
144  } else {
145  last_non_zero_p1 = 0;
146  bias = s->q_inter_matrix16[qscale][1];
147  qmat = s->q_inter_matrix16[qscale][0];
148  }
149 
150  if((s->out_format == FMT_H263 || s->out_format == FMT_H261) && s->mpeg_quant==0){
151 
152  __asm__ volatile(
153  "movd %%"REG_a", "MM"3 \n\t" // last_non_zero_p1
154  SPREADW(MM"3")
155  "pxor "MM"7, "MM"7 \n\t" // 0
156  "pxor "MM"4, "MM"4 \n\t" // 0
157  MOVQ" (%2), "MM"5 \n\t" // qmat[0]
158  "pxor "MM"6, "MM"6 \n\t"
159  "psubw (%3), "MM"6 \n\t" // -bias[0]
160  "mov $-128, %%"REG_a" \n\t"
161  ".p2align 4 \n\t"
162  "1: \n\t"
163  MOVQ" (%1, %%"REG_a"), "MM"0 \n\t" // block[i]
164  SAVE_SIGN(MM"1", MM"0") // ABS(block[i])
165  "psubusw "MM"6, "MM"0 \n\t" // ABS(block[i]) + bias[0]
166  "pmulhw "MM"5, "MM"0 \n\t" // (ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16
167  "por "MM"0, "MM"4 \n\t"
168  RESTORE_SIGN(MM"1", MM"0") // out=((ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16)*sign(block[i])
169  MOVQ" "MM"0, (%5, %%"REG_a") \n\t"
170  "pcmpeqw "MM"7, "MM"0 \n\t" // out==0 ? 0xFF : 0x00
171  MOVQ" (%4, %%"REG_a"), "MM"1 \n\t"
172  MOVQ" "MM"7, (%1, %%"REG_a") \n\t" // 0
173  "pandn "MM"1, "MM"0 \n\t"
174  PMAXW(MM"0", MM"3")
175  "add $"MMREG_WIDTH", %%"REG_a" \n\t"
176  " js 1b \n\t"
177  PMAX(MM"3", MM"0")
178  "movd "MM"3, %%"REG_a" \n\t"
179  "movzbl %%al, %%eax \n\t" // last_non_zero_p1
180  : "+a" (last_non_zero_p1)
181  : "r" (block+64), "r" (qmat), "r" (bias),
182  "r" (inv_zigzag_direct16 + 64), "r" (temp_block + 64)
183  XMM_CLOBBERS_ONLY("%xmm0", "%xmm1", "%xmm2", "%xmm3",
184  "%xmm4", "%xmm5", "%xmm6", "%xmm7")
185  );
186  }else{ // FMT_H263
187  __asm__ volatile(
188  "movd %%"REG_a", "MM"3 \n\t" // last_non_zero_p1
189  SPREADW(MM"3")
190  "pxor "MM"7, "MM"7 \n\t" // 0
191  "pxor "MM"4, "MM"4 \n\t" // 0
192  "mov $-128, %%"REG_a" \n\t"
193  ".p2align 4 \n\t"
194  "1: \n\t"
195  MOVQ" (%1, %%"REG_a"), "MM"0 \n\t" // block[i]
196  SAVE_SIGN(MM"1", MM"0") // ABS(block[i])
197  MOVQ" (%3, %%"REG_a"), "MM"6 \n\t" // bias[0]
198  "paddusw "MM"6, "MM"0 \n\t" // ABS(block[i]) + bias[0]
199  MOVQ" (%2, %%"REG_a"), "MM"5 \n\t" // qmat[i]
200  "pmulhw "MM"5, "MM"0 \n\t" // (ABS(block[i])*qmat[0] + bias[0]*qmat[0])>>16
201  "por "MM"0, "MM"4 \n\t"
202  RESTORE_SIGN(MM"1", MM"0") // out=((ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16)*sign(block[i])
203  MOVQ" "MM"0, (%5, %%"REG_a") \n\t"
204  "pcmpeqw "MM"7, "MM"0 \n\t" // out==0 ? 0xFF : 0x00
205  MOVQ" (%4, %%"REG_a"), "MM"1 \n\t"
206  MOVQ" "MM"7, (%1, %%"REG_a") \n\t" // 0
207  "pandn "MM"1, "MM"0 \n\t"
208  PMAXW(MM"0", MM"3")
209  "add $"MMREG_WIDTH", %%"REG_a" \n\t"
210  " js 1b \n\t"
211  PMAX(MM"3", MM"0")
212  "movd "MM"3, %%"REG_a" \n\t"
213  "movzbl %%al, %%eax \n\t" // last_non_zero_p1
214  : "+a" (last_non_zero_p1)
215  : "r" (block+64), "r" (qmat+64), "r" (bias+64),
216  "r" (inv_zigzag_direct16 + 64), "r" (temp_block + 64)
217  XMM_CLOBBERS_ONLY("%xmm0", "%xmm1", "%xmm2", "%xmm3",
218  "%xmm4", "%xmm5", "%xmm6", "%xmm7")
219  );
220  }
221  __asm__ volatile(
222  "movd %1, "MM"1 \n\t" // max_qcoeff
223  SPREADW(MM"1")
224  "psubusw "MM"1, "MM"4 \n\t"
225  "packuswb "MM"4, "MM"4 \n\t"
226 #if COMPILE_TEMPLATE_SSE2
227  "packsswb "MM"4, "MM"4 \n\t"
228 #endif
229  "movd "MM"4, %0 \n\t" // *overflow
230  : "=g" (*overflow)
231  : "g" (s->max_qcoeff)
232  );
233 
234  if(s->mb_intra) block[0]= level;
235  else block[0]= temp_block[0];
236 
237  if (s->idsp.perm_type == FF_IDCT_PERM_SIMPLE) {
238  if(last_non_zero_p1 <= 1) goto end;
239  block[0x08] = temp_block[0x01]; block[0x10] = temp_block[0x08];
240  block[0x20] = temp_block[0x10];
241  if(last_non_zero_p1 <= 4) goto end;
242  block[0x18] = temp_block[0x09]; block[0x04] = temp_block[0x02];
243  block[0x09] = temp_block[0x03];
244  if(last_non_zero_p1 <= 7) goto end;
245  block[0x14] = temp_block[0x0A]; block[0x28] = temp_block[0x11];
246  block[0x12] = temp_block[0x18]; block[0x02] = temp_block[0x20];
247  if(last_non_zero_p1 <= 11) goto end;
248  block[0x1A] = temp_block[0x19]; block[0x24] = temp_block[0x12];
249  block[0x19] = temp_block[0x0B]; block[0x01] = temp_block[0x04];
250  block[0x0C] = temp_block[0x05];
251  if(last_non_zero_p1 <= 16) goto end;
252  block[0x11] = temp_block[0x0C]; block[0x29] = temp_block[0x13];
253  block[0x16] = temp_block[0x1A]; block[0x0A] = temp_block[0x21];
254  block[0x30] = temp_block[0x28]; block[0x22] = temp_block[0x30];
255  block[0x38] = temp_block[0x29]; block[0x06] = temp_block[0x22];
256  if(last_non_zero_p1 <= 24) goto end;
257  block[0x1B] = temp_block[0x1B]; block[0x21] = temp_block[0x14];
258  block[0x1C] = temp_block[0x0D]; block[0x05] = temp_block[0x06];
259  block[0x0D] = temp_block[0x07]; block[0x15] = temp_block[0x0E];
260  block[0x2C] = temp_block[0x15]; block[0x13] = temp_block[0x1C];
261  if(last_non_zero_p1 <= 32) goto end;
262  block[0x0B] = temp_block[0x23]; block[0x34] = temp_block[0x2A];
263  block[0x2A] = temp_block[0x31]; block[0x32] = temp_block[0x38];
264  block[0x3A] = temp_block[0x39]; block[0x26] = temp_block[0x32];
265  block[0x39] = temp_block[0x2B]; block[0x03] = temp_block[0x24];
266  if(last_non_zero_p1 <= 40) goto end;
267  block[0x1E] = temp_block[0x1D]; block[0x25] = temp_block[0x16];
268  block[0x1D] = temp_block[0x0F]; block[0x2D] = temp_block[0x17];
269  block[0x17] = temp_block[0x1E]; block[0x0E] = temp_block[0x25];
270  block[0x31] = temp_block[0x2C]; block[0x2B] = temp_block[0x33];
271  if(last_non_zero_p1 <= 48) goto end;
272  block[0x36] = temp_block[0x3A]; block[0x3B] = temp_block[0x3B];
273  block[0x23] = temp_block[0x34]; block[0x3C] = temp_block[0x2D];
274  block[0x07] = temp_block[0x26]; block[0x1F] = temp_block[0x1F];
275  block[0x0F] = temp_block[0x27]; block[0x35] = temp_block[0x2E];
276  if(last_non_zero_p1 <= 56) goto end;
277  block[0x2E] = temp_block[0x35]; block[0x33] = temp_block[0x3C];
278  block[0x3E] = temp_block[0x3D]; block[0x27] = temp_block[0x36];
279  block[0x3D] = temp_block[0x2F]; block[0x2F] = temp_block[0x37];
280  block[0x37] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
281  }else if(s->idsp.perm_type == FF_IDCT_PERM_LIBMPEG2){
282  if(last_non_zero_p1 <= 1) goto end;
283  block[0x04] = temp_block[0x01];
284  block[0x08] = temp_block[0x08]; block[0x10] = temp_block[0x10];
285  if(last_non_zero_p1 <= 4) goto end;
286  block[0x0C] = temp_block[0x09]; block[0x01] = temp_block[0x02];
287  block[0x05] = temp_block[0x03];
288  if(last_non_zero_p1 <= 7) goto end;
289  block[0x09] = temp_block[0x0A]; block[0x14] = temp_block[0x11];
290  block[0x18] = temp_block[0x18]; block[0x20] = temp_block[0x20];
291  if(last_non_zero_p1 <= 11) goto end;
292  block[0x1C] = temp_block[0x19];
293  block[0x11] = temp_block[0x12]; block[0x0D] = temp_block[0x0B];
294  block[0x02] = temp_block[0x04]; block[0x06] = temp_block[0x05];
295  if(last_non_zero_p1 <= 16) goto end;
296  block[0x0A] = temp_block[0x0C]; block[0x15] = temp_block[0x13];
297  block[0x19] = temp_block[0x1A]; block[0x24] = temp_block[0x21];
298  block[0x28] = temp_block[0x28]; block[0x30] = temp_block[0x30];
299  block[0x2C] = temp_block[0x29]; block[0x21] = temp_block[0x22];
300  if(last_non_zero_p1 <= 24) goto end;
301  block[0x1D] = temp_block[0x1B]; block[0x12] = temp_block[0x14];
302  block[0x0E] = temp_block[0x0D]; block[0x03] = temp_block[0x06];
303  block[0x07] = temp_block[0x07]; block[0x0B] = temp_block[0x0E];
304  block[0x16] = temp_block[0x15]; block[0x1A] = temp_block[0x1C];
305  if(last_non_zero_p1 <= 32) goto end;
306  block[0x25] = temp_block[0x23]; block[0x29] = temp_block[0x2A];
307  block[0x34] = temp_block[0x31]; block[0x38] = temp_block[0x38];
308  block[0x3C] = temp_block[0x39]; block[0x31] = temp_block[0x32];
309  block[0x2D] = temp_block[0x2B]; block[0x22] = temp_block[0x24];
310  if(last_non_zero_p1 <= 40) goto end;
311  block[0x1E] = temp_block[0x1D]; block[0x13] = temp_block[0x16];
312  block[0x0F] = temp_block[0x0F]; block[0x17] = temp_block[0x17];
313  block[0x1B] = temp_block[0x1E]; block[0x26] = temp_block[0x25];
314  block[0x2A] = temp_block[0x2C]; block[0x35] = temp_block[0x33];
315  if(last_non_zero_p1 <= 48) goto end;
316  block[0x39] = temp_block[0x3A]; block[0x3D] = temp_block[0x3B];
317  block[0x32] = temp_block[0x34]; block[0x2E] = temp_block[0x2D];
318  block[0x23] = temp_block[0x26]; block[0x1F] = temp_block[0x1F];
319  block[0x27] = temp_block[0x27]; block[0x2B] = temp_block[0x2E];
320  if(last_non_zero_p1 <= 56) goto end;
321  block[0x36] = temp_block[0x35]; block[0x3A] = temp_block[0x3C];
322  block[0x3E] = temp_block[0x3D]; block[0x33] = temp_block[0x36];
323  block[0x2F] = temp_block[0x2F]; block[0x37] = temp_block[0x37];
324  block[0x3B] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
325  }else{
326  if(last_non_zero_p1 <= 1) goto end;
327  block[0x01] = temp_block[0x01];
328  block[0x08] = temp_block[0x08]; block[0x10] = temp_block[0x10];
329  if(last_non_zero_p1 <= 4) goto end;
330  block[0x09] = temp_block[0x09]; block[0x02] = temp_block[0x02];
331  block[0x03] = temp_block[0x03];
332  if(last_non_zero_p1 <= 7) goto end;
333  block[0x0A] = temp_block[0x0A]; block[0x11] = temp_block[0x11];
334  block[0x18] = temp_block[0x18]; block[0x20] = temp_block[0x20];
335  if(last_non_zero_p1 <= 11) goto end;
336  block[0x19] = temp_block[0x19];
337  block[0x12] = temp_block[0x12]; block[0x0B] = temp_block[0x0B];
338  block[0x04] = temp_block[0x04]; block[0x05] = temp_block[0x05];
339  if(last_non_zero_p1 <= 16) goto end;
340  block[0x0C] = temp_block[0x0C]; block[0x13] = temp_block[0x13];
341  block[0x1A] = temp_block[0x1A]; block[0x21] = temp_block[0x21];
342  block[0x28] = temp_block[0x28]; block[0x30] = temp_block[0x30];
343  block[0x29] = temp_block[0x29]; block[0x22] = temp_block[0x22];
344  if(last_non_zero_p1 <= 24) goto end;
345  block[0x1B] = temp_block[0x1B]; block[0x14] = temp_block[0x14];
346  block[0x0D] = temp_block[0x0D]; block[0x06] = temp_block[0x06];
347  block[0x07] = temp_block[0x07]; block[0x0E] = temp_block[0x0E];
348  block[0x15] = temp_block[0x15]; block[0x1C] = temp_block[0x1C];
349  if(last_non_zero_p1 <= 32) goto end;
350  block[0x23] = temp_block[0x23]; block[0x2A] = temp_block[0x2A];
351  block[0x31] = temp_block[0x31]; block[0x38] = temp_block[0x38];
352  block[0x39] = temp_block[0x39]; block[0x32] = temp_block[0x32];
353  block[0x2B] = temp_block[0x2B]; block[0x24] = temp_block[0x24];
354  if(last_non_zero_p1 <= 40) goto end;
355  block[0x1D] = temp_block[0x1D]; block[0x16] = temp_block[0x16];
356  block[0x0F] = temp_block[0x0F]; block[0x17] = temp_block[0x17];
357  block[0x1E] = temp_block[0x1E]; block[0x25] = temp_block[0x25];
358  block[0x2C] = temp_block[0x2C]; block[0x33] = temp_block[0x33];
359  if(last_non_zero_p1 <= 48) goto end;
360  block[0x3A] = temp_block[0x3A]; block[0x3B] = temp_block[0x3B];
361  block[0x34] = temp_block[0x34]; block[0x2D] = temp_block[0x2D];
362  block[0x26] = temp_block[0x26]; block[0x1F] = temp_block[0x1F];
363  block[0x27] = temp_block[0x27]; block[0x2E] = temp_block[0x2E];
364  if(last_non_zero_p1 <= 56) goto end;
365  block[0x35] = temp_block[0x35]; block[0x3C] = temp_block[0x3C];
366  block[0x3D] = temp_block[0x3D]; block[0x36] = temp_block[0x36];
367  block[0x2F] = temp_block[0x2F]; block[0x37] = temp_block[0x37];
368  block[0x3E] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
369  }
370  end:
371  return last_non_zero_p1 - 1;
372 }
#define PMAXW(a, b)
const char * s
Definition: avisynth_c.h:631
#define RENAME(a)
#define MOVQ
mpegvideo header.
static uint16_t inv_zigzag_direct16[64]
Definition: mpegvideoenc.c:31
#define PMAX(a, b)
const uint32_t ff_inverse[257]
Definition: mathtables.c:27
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:63
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
#define RESTORE_SIGN(a, b)
#define MM
common internal API header
int n
Definition: avisynth_c.h:547
#define SAVE_SIGN(a, b)
uint8_t level
Definition: svq3.c:150
MpegEncContext.
Definition: mpegvideo.h:78
#define XMM_CLOBBERS_ONLY(...)
Definition: asm.h:99
#define SPREADW(a)
int x86_reg
Definition: asm.h:72
#define LOCAL_ALIGNED_16(t, v,...)
Definition: internal.h:121
int dummy
Definition: motion-test.c:64
#define MMREG_WIDTH
static int16_t block[64]
Definition: dct-test.c:112