1 /*
2 * Lossless video DSP utils
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #include "avcodec.h"
21 #include "lossless_videodsp.h"
22 #include "libavcodec/mathops.h"
23
24 // 0x7f7f7f7f or 0x7f7f7f7f7f7f7f7f or whatever, depending on the cpu's native arithmetic size
25 #define pb_7f (~0UL / 255 * 0x7f)
26 #define pb_80 (~0UL / 255 * 0x80)
27
add_bytes_c(uint8_t * dst,uint8_t * src,ptrdiff_t w)28 static void add_bytes_c(uint8_t *dst, uint8_t *src, ptrdiff_t w)
29 {
30 long i;
31
32 for (i = 0; i <= w - (int) sizeof(long); i += sizeof(long)) {
33 long a = *(long *) (src + i);
34 long b = *(long *) (dst + i);
35 *(long *) (dst + i) = ((a & pb_7f) + (b & pb_7f)) ^ ((a ^ b) & pb_80);
36 }
37 for (; i < w; i++)
38 dst[i + 0] += src[i + 0];
39 }
40
add_median_pred_c(uint8_t * dst,const uint8_t * src1,const uint8_t * diff,ptrdiff_t w,int * left,int * left_top)41 static void add_median_pred_c(uint8_t *dst, const uint8_t *src1,
42 const uint8_t *diff, ptrdiff_t w,
43 int *left, int *left_top)
44 {
45 int i;
46 uint8_t l, lt;
47
48 l = *left;
49 lt = *left_top;
50
51 for (i = 0; i < w; i++) {
52 l = mid_pred(l, src1[i], (l + src1[i] - lt) & 0xFF) + diff[i];
53 lt = src1[i];
54 dst[i] = l;
55 }
56
57 *left = l;
58 *left_top = lt;
59 }
60
add_left_pred_c(uint8_t * dst,const uint8_t * src,ptrdiff_t w,int acc)61 static int add_left_pred_c(uint8_t *dst, const uint8_t *src, ptrdiff_t w,
62 int acc)
63 {
64 int i;
65
66 for (i = 0; i < w - 1; i++) {
67 acc += src[i];
68 dst[i] = acc;
69 i++;
70 acc += src[i];
71 dst[i] = acc;
72 }
73
74 for (; i < w; i++) {
75 acc += src[i];
76 dst[i] = acc;
77 }
78
79 return acc;
80 }
81
add_left_pred_int16_c(uint16_t * dst,const uint16_t * src,unsigned mask,ptrdiff_t w,unsigned acc)82 static int add_left_pred_int16_c(uint16_t *dst, const uint16_t *src, unsigned mask, ptrdiff_t w, unsigned acc){
83 int i;
84
85 for(i=0; i<w-1; i++){
86 acc+= src[i];
87 dst[i]= acc &= mask;
88 i++;
89 acc+= src[i];
90 dst[i]= acc &= mask;
91 }
92
93 for(; i<w; i++){
94 acc+= src[i];
95 dst[i]= acc &= mask;
96 }
97
98 return acc;
99 }
100
add_gradient_pred_c(uint8_t * src,const ptrdiff_t stride,const ptrdiff_t width)101 static void add_gradient_pred_c(uint8_t *src, const ptrdiff_t stride, const ptrdiff_t width){
102 int A, B, C, i;
103
104 for (i = 0; i < width; i++) {
105 A = src[i - stride];
106 B = src[i - (stride + 1)];
107 C = src[i - 1];
108 src[i] = (A - B + C + src[i]) & 0xFF;
109 }
110 }
111
ff_llviddsp_init(LLVidDSPContext * c)112 void ff_llviddsp_init(LLVidDSPContext *c)
113 {
114 c->add_bytes = add_bytes_c;
115 c->add_median_pred = add_median_pred_c;
116 c->add_left_pred = add_left_pred_c;
117
118 c->add_left_pred_int16 = add_left_pred_int16_c;
119 c->add_gradient_pred = add_gradient_pred_c;
120
121 if (ARCH_PPC)
122 ff_llviddsp_init_ppc(c);
123 if (ARCH_X86)
124 ff_llviddsp_init_x86(c);
125 }
126