summaryrefslogtreecommitdiff
path: root/codecs/sbc/sbc.c
blob: b14b5bb6812bc35a488f9b14df68ed2b24b43093 (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
134
135
136
137
138
/* sbc.c
 * Support for external Bluetooth SBC codec
 *
 * Copyright 2012, Michal Labedzki for Tieto Corporation
 *
 * $Id$
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#ifdef HAVE_SBC

#include <glib.h>
#include <sbc/sbc.h>

#include "sbc.h"

#define SBC_BUFFER 8192

void *
codec_sbc_init(void)
{
    sbc_t *sbc;

    sbc = g_malloc(sizeof(sbc_t));
    sbc_init(sbc, 0L);

    return sbc;
}

void
codec_sbc_release(void *ctx)
{
    sbc_t *sbc = (sbc_t *) ctx;

    sbc_finish(sbc);
    g_free(sbc);
}

int
codec_sbc_get_channels(void *ctx)
{
    sbc_t *sbc = (sbc_t *) ctx;
    if (sbc->mode == SBC_MODE_MONO)
        return 1;

    return 2;
}

int
codec_sbc_get_frequency(void *ctx)
{
    sbc_t *sbc = (sbc_t *) ctx;
    int frequency;

    switch (sbc->frequency) {
    case SBC_FREQ_16000:
        frequency = 16000;
        break;

    case SBC_FREQ_32000:
        frequency = 32000;
        break;

    case SBC_FREQ_44100:
        frequency = 44100;
        break;

    case SBC_FREQ_48000:
        frequency = 48000;
        break;
    default:
        frequency = 0;
    }

    return frequency;
}

int
codec_sbc_decode(void *ctx, const void *input, int inputSizeBytes, void *output,
        int *outputSizeBytes)
{
    size_t  size_in = (size_t) inputSizeBytes;
    size_t  size_out = SBC_BUFFER;
    size_t  len;
    int     framelen;
    int     xframe_pos = 0;
    guint8  *data_in  = (guint8 *) input;
    guint8  *data_out = (guint8 *) output;
    sbc_t   *sbc = (sbc_t *) ctx;
    guint8  *i_data;
    guint8  tmp;

    if (!output || !outputSizeBytes) {
        return size_out;
    }

    sbc->endian = SBC_BE;

    framelen = 0;
    *outputSizeBytes = 0;
    while (xframe_pos < inputSizeBytes) {
        framelen = sbc_decode(sbc, data_in, size_in, data_out, size_out, &len);
        xframe_pos += framelen;
        data_in += framelen;
        *outputSizeBytes += len;

        for (i_data = data_out; i_data < data_out + len; i_data += 2) {
                tmp = i_data[0];
                i_data[0] = i_data[1];
                i_data[1] = tmp;
        }

        data_out += len;
    }

    return *outputSizeBytes;
}

#endif