summaryrefslogtreecommitdiffstats
path: root/src/zero.c
blob: 0cceb23376c4e950a64ac89d34ec44b634c24d28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/***
  This file is part of libsydney.

  Copyright 2007-2008 Lennart Poettering

  libsydney is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation, either version 2.1 of the
  License, or (at your option) any later version.

  libsydney is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with libsydney. If not, see
  <http://www.gnu.org/licenses/>.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>

#include "macro.h"
#include "zero.h"

static void zero_u8(void *dst, size_t dstr, size_t bytes) {
    uint8_t *d = dst;

    if (dstr == 1)
        memset(dst, 0x80, bytes);
    else {
        for (; bytes > 0; bytes --, d += dstr)
            *d = 0x80;
    }
}

static void zero_ulaw(void *dst, size_t dstr, size_t bytes) {
    uint8_t *d = dst;

    if (dstr == 1)
        memset(dst, 0xff, bytes);
    else {
        for (; bytes > 0; bytes --, d += dstr)
            *d = 0xff;
    }
}

static void zero_alaw(void *dst, size_t dstr, size_t bytes) {
    uint8_t *d = dst;

    if (dstr == 1)
        memset(dst, 0xd5, bytes);
    else {
        for (; bytes > 0; bytes --, d += dstr)
            *d = 0xd5;
    }
}

static void zero_16(void *dst, size_t dstr, size_t bytes) {
    uint16_t *d = dst;
    size_t n = bytes/sizeof(uint16_t);

    if (dstr == sizeof(uint16_t))
        memset(dst, 0, bytes);
    else {
        for (; n > 0; n --, d += dstr/sizeof(uint16_t))
            *d = 0;
    }
}

static void zero_24(void *dst, size_t dstr, size_t bytes) {
    uint8_t *d = dst;
    size_t n = bytes/3;

    if (dstr == 3)
        memset(dst, 0, bytes);
    else {
        for (; n > 0; n --, d += dstr/3)
            d[0] = d[1] = d[2] = 0;
    }
}

static void zero_32(void *dst, size_t dstr, size_t bytes) {
    uint32_t *d = dst;
    size_t n = bytes/sizeof(float);

    if (dstr == sizeof(uint32_t))
        memset(dst, 0, bytes);
    else {
        for (; n > 0; n --, d += dstr/sizeof(uint32_t))
            *d = 0;
    }
}

sa_zero_func_t sa_get_zero_func(sa_pcm_format_t f) {

    static const sa_zero_func_t funcs[_SA_PCM_FORMAT_MAX] = {
        [SA_PCM_FORMAT_U8] = zero_u8,
        [SA_PCM_FORMAT_ULAW] = zero_ulaw,
        [SA_PCM_FORMAT_ALAW] = zero_alaw,
        [SA_PCM_FORMAT_S16_LE] = zero_16,
        [SA_PCM_FORMAT_S16_BE] = zero_16,
        [SA_PCM_FORMAT_S24_LE] = zero_24,
        [SA_PCM_FORMAT_S24_BE] = zero_24,
        [SA_PCM_FORMAT_S32_LE] = zero_32,
        [SA_PCM_FORMAT_S32_BE] = zero_32,
        [SA_PCM_FORMAT_FLOAT32_LE] = zero_32,
        [SA_PCM_FORMAT_FLOAT32_BE] = zero_32
    };

    sa_assert(f < _SA_PCM_FORMAT_MAX);

    return funcs[f];
}