]> git.sesse.net Git - ffmpeg/blob - libavcodec/ppc/mpegvideo_altivec.c
avconv: fix a segfault on -c copy with -filter_complex.
[ffmpeg] / libavcodec / ppc / mpegvideo_altivec.c
1 /*
2  * Copyright (c) 2002 Dieter Shirley
3  *
4  * dct_unquantize_h263_altivec:
5  * Copyright (c) 2003 Romain Dolbeau <romain@dolbeau.org>
6  *
7  * This file is part of Libav.
8  *
9  * Libav is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * Libav is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with Libav; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include "libavutil/cpu.h"
27 #include "libavcodec/dsputil.h"
28 #include "libavcodec/mpegvideo.h"
29
30 #include "util_altivec.h"
31 #include "types_altivec.h"
32 #include "dsputil_altivec.h"
33
34 /* AltiVec version of dct_unquantize_h263
35    this code assumes `block' is 16 bytes-aligned */
36 static void dct_unquantize_h263_altivec(MpegEncContext *s,
37                                  DCTELEM *block, int n, int qscale)
38 {
39     int i, level, qmul, qadd;
40     int nCoeffs;
41
42     assert(s->block_last_index[n]>=0);
43
44     qadd = (qscale - 1) | 1;
45     qmul = qscale << 1;
46
47     if (s->mb_intra) {
48         if (!s->h263_aic) {
49             if (n < 4)
50                 block[0] = block[0] * s->y_dc_scale;
51             else
52                 block[0] = block[0] * s->c_dc_scale;
53         }else
54             qadd = 0;
55         i = 1;
56         nCoeffs= 63; //does not always use zigzag table
57     } else {
58         i = 0;
59         nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
60     }
61
62     {
63         register const vector signed short vczero = (const vector signed short)vec_splat_s16(0);
64         DECLARE_ALIGNED(16, short, qmul8) = qmul;
65         DECLARE_ALIGNED(16, short, qadd8) = qadd;
66         register vector signed short blockv, qmulv, qaddv, nqaddv, temp1;
67         register vector bool short blockv_null, blockv_neg;
68         register short backup_0 = block[0];
69         register int j = 0;
70
71         qmulv = vec_splat((vec_s16)vec_lde(0, &qmul8), 0);
72         qaddv = vec_splat((vec_s16)vec_lde(0, &qadd8), 0);
73         nqaddv = vec_sub(vczero, qaddv);
74
75         // vectorize all the 16 bytes-aligned blocks
76         // of 8 elements
77         for(; (j + 7) <= nCoeffs ; j+=8) {
78             blockv = vec_ld(j << 1, block);
79             blockv_neg = vec_cmplt(blockv, vczero);
80             blockv_null = vec_cmpeq(blockv, vczero);
81             // choose between +qadd or -qadd as the third operand
82             temp1 = vec_sel(qaddv, nqaddv, blockv_neg);
83             // multiply & add (block{i,i+7} * qmul [+-] qadd)
84             temp1 = vec_mladd(blockv, qmulv, temp1);
85             // put 0 where block[{i,i+7} used to have 0
86             blockv = vec_sel(temp1, blockv, blockv_null);
87             vec_st(blockv, j << 1, block);
88         }
89
90         // if nCoeffs isn't a multiple of 8, finish the job
91         // using good old scalar units.
92         // (we could do it using a truncated vector,
93         // but I'm not sure it's worth the hassle)
94         for(; j <= nCoeffs ; j++) {
95             level = block[j];
96             if (level) {
97                 if (level < 0) {
98                     level = level * qmul - qadd;
99                 } else {
100                     level = level * qmul + qadd;
101                 }
102                 block[j] = level;
103             }
104         }
105
106         if (i == 1) {
107             // cheat. this avoid special-casing the first iteration
108             block[0] = backup_0;
109         }
110     }
111 }
112
113
114 void ff_MPV_common_init_altivec(MpegEncContext *s)
115 {
116     if (!(av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC)) return;
117
118     if ((s->avctx->dct_algo == FF_DCT_AUTO) ||
119             (s->avctx->dct_algo == FF_DCT_ALTIVEC)) {
120         s->dct_unquantize_h263_intra = dct_unquantize_h263_altivec;
121         s->dct_unquantize_h263_inter = dct_unquantize_h263_altivec;
122     }
123 }