FFmpeg
pngdsp.c
Go to the documentation of this file.
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 #define RN AV_JOIN(AV_RN, BITS)
38 #define RNA AV_JOIN(AV_JOIN(AV_RN, BITS), A)
39 #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 {
48  long i;
49  for (i = 0; i <= w - (int) sizeof(uint_native); i += sizeof(uint_native)) {
50  uint_native a = RNA(src1 + i);
51  uint_native b = RN (src2 + i);
52  WN(dst + i, ((a & pb_7f) + (b & pb_7f)) ^ ((a ^ b) & pb_80));
53  }
54  for (; i < w; i++)
55  dst[i] = src1[i] + src2[i];
56 }
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 {
61  for (int i = 0; i < w; ++i) {
62  int a, b, c, p, pa, pb, pc;
63 
64  a = dst[i - bpp];
65  b = top[i];
66  c = top[i - bpp];
67 
68  p = b - c;
69  pc = a - c;
70 
71  pa = abs(p);
72  pb = abs(pc);
73  pc = abs(p + pc);
74 
75  if (pa <= pb && pa <= pc)
76  p = a;
77  else if (pb <= pc)
78  p = b;
79  else
80  p = c;
81  dst[i] = p + src[i];
82  }
83 }
84 
86 {
89 
90 #if ARCH_X86 && HAVE_X86ASM
91  ff_pngdsp_init_x86(dsp);
92 #endif
93 }
PNGDSPContext
Definition: pngdsp.h:29
pb_80
#define pb_80
Definition: pngdsp.c:43
RNA
#define RNA
Definition: pngdsp.c:38
src1
const pixel * src1
Definition: h264pred_template.c:420
RN
#define RN
Definition: pngdsp.c:37
ff_pngdsp_init_x86
void ff_pngdsp_init_x86(PNGDSPContext *dsp)
Definition: pngdsp_init.c:32
b
#define b
Definition: input.c:42
pb_7f
#define pb_7f
Definition: pngdsp.c:42
macros.h
pngdsp.h
av_cold
#define av_cold
Definition: attributes.h:106
WN
#define WN
Definition: pngdsp.c:39
intreadwrite.h
add_bytes_l2_c
static void add_bytes_l2_c(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int w)
Definition: pngdsp.c:45
abs
#define abs(x)
Definition: cuda_runtime.h:35
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
attributes.h
ff_pngdsp_init
av_cold void ff_pngdsp_init(PNGDSPContext *dsp)
Definition: pngdsp.c:85
PNGDSPContext::add_bytes_l2
void(* add_bytes_l2)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int w)
Definition: pngdsp.h:30
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
src2
const pixel * src2
Definition: h264pred_template.c:421
PNGDSPContext::add_paeth_prediction
void(* add_paeth_prediction)(uint8_t *dst, const uint8_t *src, const uint8_t *top, int w, int bpp)
Definition: pngdsp.h:35
uint_native
uint32_t uint_native
Definition: pngdsp.c:35
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.hpp:53
w
uint8_t w
Definition: llvidencdsp.c:39
ff_png_add_paeth_prediction
void ff_png_add_paeth_prediction(uint8_t *dst, const uint8_t *src, const uint8_t *top, int w, int bpp)
Definition: pngdsp.c:58
uint_native
uint32_t uint_native
Definition: huffyuvencdsp.c:30
src
#define src
Definition: vp8dsp.c:248