summaryrefslogtreecommitdiffstats
path: root/audio/filter/af_convert24.c
blob: 56b405a7e91ff76d9b39010d828bcc86229f63ce (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * This file is part of mpv.
 *
 * mpv is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * mpv 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with mpv.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <assert.h>

#include "audio/format.h"
#include "af.h"

static bool test_conversion(int src_format, int dst_format)
{
    if (!(src_format & AF_FORMAT_POINT_MASK) == AF_FORMAT_I)
        return false;
    if ((src_format & ~AF_FORMAT_BITS_MASK) !=
        (dst_format & ~AF_FORMAT_BITS_MASK))
        return false;
    int srcbits = src_format & AF_FORMAT_BITS_MASK;
    int dstbits = dst_format & AF_FORMAT_BITS_MASK;
    return (srcbits == AF_FORMAT_24BIT && dstbits == AF_FORMAT_32BIT) ||
           (srcbits == AF_FORMAT_32BIT && dstbits == AF_FORMAT_24BIT);
}

static int control(struct af_instance *af, int cmd, void *arg)
{
    switch (cmd) {
    case AF_CONTROL_REINIT: {
        struct mp_audio *in = arg;
        struct mp_audio orig_in = *in;
        struct mp_audio *out = af->data;

        if (!test_conversion(in->format, out->format))
            return AF_DETACH;

        if ((in->format & AF_FORMAT_BITS_MASK) == AF_FORMAT_24BIT) {
            mp_audio_set_format(out, af_fmt_change_bits(in->format, 32));
        } else if ((in->format & AF_FORMAT_BITS_MASK) == AF_FORMAT_32BIT) {
            mp_audio_set_format(out, af_fmt_change_bits(in->format, 24));
        } else {
            abort();
        }

        out->rate = in->rate;
        mp_audio_set_channels(out, &in->channels);

        assert(test_conversion(in->format, out->format));

        af->mul = (double)out->bps / in->bps;

        return mp_audio_config_equals(in, &orig_in) ? AF_OK : AF_FALSE;
    }
    case AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET: {
        mp_audio_set_format(af->data, *(int*)arg);
        return AF_OK;
    }
    }
    return AF_UNKNOWN;
}

// The LSB is always ignored.
#if BYTE_ORDER == BIG_ENDIAN
#define SHIFT(x) ((3-(x))*8)
#else
#define SHIFT(x) (((x)+1)*8)
#endif

static struct mp_audio *play(struct af_instance *af, struct mp_audio *data)
{
    if (RESIZE_LOCAL_BUFFER(af, data) != AF_OK)
        return NULL;

    struct mp_audio *out = af->data;
    size_t len = data->len / data->bps;

    if (data->bps == 4) {
        for (int s = 0; s < len; s++) {
            uint32_t val = *((uint32_t *)data->audio + s);
            uint8_t *ptr = (uint8_t *)out->audio + s * 3;
            ptr[0] = val >> SHIFT(0);
            ptr[1] = val >> SHIFT(1);
            ptr[2] = val >> SHIFT(2);
        }
        mp_audio_set_format(data, af_fmt_change_bits(data->format, 24));
    } else {
        for (int s = 0; s < len; s++) {
            uint8_t *ptr = (uint8_t *)data->audio + s * 3;
            uint32_t val = ptr[0] << SHIFT(0)
                         | ptr[1] << SHIFT(1)
                         | ptr[2] << SHIFT(2);
            *((uint32_t *)out->audio + s) = val;
        }
        mp_audio_set_format(data, af_fmt_change_bits(data->format, 32));
    }

    data->audio = out->audio;
    data->len = len * data->bps;
    return data;
}

static void uninit(struct af_instance* af)
{
    if (af->data)
        free(af->data->audio);
}

static int af_open(struct af_instance *af)
{
    af->control = control;
    af->play = play;
    af->uninit = uninit;
    af->data = talloc_zero(af, struct mp_audio);
    return AF_OK;
}

struct af_info af_info_convert24 = {
    .info = "Convert between 24 and 32 bit sample format",
    .name = "convert24",
    .open = af_open,
    .test_conversion = test_conversion,
};