/src/ffmpeg/libavcodec/pngdsp.c
Line | Count | Source |
1 | | /* |
2 | | * PNG image format |
3 | | * Copyright (c) 2008 Loren Merrit <lorenm@u.washington.edu> |
4 | | * |
5 | | * This file is part of FFmpeg. |
6 | | * |
7 | | * FFmpeg is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * FFmpeg is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public |
18 | | * License along with FFmpeg; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | | */ |
21 | | |
22 | | #include <stdlib.h> |
23 | | |
24 | | #include "config.h" |
25 | | #include "libavutil/attributes.h" |
26 | | #include "libavutil/intreadwrite.h" |
27 | | #include "libavutil/macros.h" |
28 | | #include "pngdsp.h" |
29 | | |
30 | | #if HAVE_FAST_64BIT |
31 | | #define BITS 64 |
32 | | typedef uint64_t uint_native; |
33 | | #else |
34 | | #define BITS 32 |
35 | | typedef uint32_t uint_native; |
36 | | #endif |
37 | 2.96M | #define RN AV_JOIN(AV_RN, BITS) |
38 | 2.96M | #define RNA AV_JOIN(AV_JOIN(AV_RN, BITS), A) |
39 | 2.96M | #define WN AV_JOIN(AV_WN, BITS) |
40 | | |
41 | | // 0x7f7f7f7f or 0x7f7f7f7f7f7f7f7f or whatever, depending on the cpu's native arithmetic size |
42 | | #define pb_7f (~(uint_native)0 / 255 * 0x7f) |
43 | | #define pb_80 (~(uint_native)0 / 255 * 0x80) |
44 | | |
45 | | static void add_bytes_l2_c(uint8_t *dst, const uint8_t *src1, |
46 | | const uint8_t *src2, int w) |
47 | 79.2k | { |
48 | 79.2k | long i; |
49 | 3.04M | for (i = 0; i <= w - (int) sizeof(uint_native); i += sizeof(uint_native)) { |
50 | 2.96M | uint_native a = RNA(src1 + i); |
51 | 2.96M | uint_native b = RN (src2 + i); |
52 | 2.96M | WN(dst + i, ((a & pb_7f) + (b & pb_7f)) ^ ((a ^ b) & pb_80)); |
53 | 2.96M | } |
54 | 148k | for (; i < w; i++) |
55 | 69.2k | dst[i] = src1[i] + src2[i]; |
56 | 79.2k | } |
57 | | |
58 | | void ff_png_add_paeth_prediction(uint8_t *dst, const uint8_t *src, |
59 | | const uint8_t *top, int w, int bpp) |
60 | 64.7k | { |
61 | 5.72M | for (int i = 0; i < w; ++i) { |
62 | 5.66M | int a, b, c, p, pa, pb, pc; |
63 | | |
64 | 5.66M | a = dst[i - bpp]; |
65 | 5.66M | b = top[i]; |
66 | 5.66M | c = top[i - bpp]; |
67 | | |
68 | 5.66M | p = b - c; |
69 | 5.66M | pc = a - c; |
70 | | |
71 | 5.66M | pa = abs(p); |
72 | 5.66M | pb = abs(pc); |
73 | 5.66M | pc = abs(p + pc); |
74 | | |
75 | 5.66M | if (pa <= pb && pa <= pc) |
76 | 4.94M | p = a; |
77 | 723k | else if (pb <= pc) |
78 | 656k | p = b; |
79 | 66.7k | else |
80 | 66.7k | p = c; |
81 | 5.66M | dst[i] = p + src[i]; |
82 | 5.66M | } |
83 | 64.7k | } |
84 | | |
85 | | av_cold void ff_pngdsp_init(PNGDSPContext *dsp) |
86 | 13.4k | { |
87 | 13.4k | dsp->add_bytes_l2 = add_bytes_l2_c; |
88 | 13.4k | dsp->add_paeth_prediction = ff_png_add_paeth_prediction; |
89 | | |
90 | | #if ARCH_X86 && HAVE_X86ASM |
91 | | ff_pngdsp_init_x86(dsp); |
92 | | #endif |
93 | 13.4k | } |