Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / plugins / codecs / G726 / G726decode.c
blob929652511ef59c5aaabe2e09de2c55b2da4fa612
1 /* G726decode.c
2 * G.726 codec
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
11 #include "config.h"
13 #include <glib.h>
15 #include "spandsp.h"
16 #include "wsutil/codecs.h"
17 #include "ws_attributes.h"
19 typedef struct _g726_codec_ctx {
20 g726_state_t *state;
21 int bit_rate;
22 } g726_codec_ctx;
24 void codec_register_g726(void);
26 static void *
27 codec_g726_init(int bit_rate, int packing)
29 g726_state_t *decoder = g726_init(NULL, bit_rate, G726_ENCODING_LINEAR, packing);
31 if (!decoder) {
32 return NULL; /* out-of-memory; */
35 g726_codec_ctx *state = g_new(g726_codec_ctx, 1);
36 state->state = decoder;
37 state->bit_rate = bit_rate;
39 return state;
42 static void *codec_g726_16_init(codec_context_t *ctx _U_) { return codec_g726_init(16000, G726_PACKING_RIGHT); }
43 static void *codec_g726_24_init(codec_context_t *ctx _U_) { return codec_g726_init(24000, G726_PACKING_RIGHT); }
44 static void *codec_g726_32_init(codec_context_t *ctx _U_) { return codec_g726_init(32000, G726_PACKING_RIGHT); }
45 static void *codec_g726_40_init(codec_context_t *ctx _U_) { return codec_g726_init(40000, G726_PACKING_RIGHT); }
46 static void *codec_aal2_g726_16_init(codec_context_t *ctx _U_) { return codec_g726_init(16000, G726_PACKING_LEFT); }
47 static void *codec_aal2_g726_24_init(codec_context_t *ctx _U_) { return codec_g726_init(24000, G726_PACKING_LEFT); }
48 static void *codec_aal2_g726_32_init(codec_context_t *ctx _U_) { return codec_g726_init(32000, G726_PACKING_LEFT); }
49 static void *codec_aal2_g726_40_init(codec_context_t *ctx _U_) { return codec_g726_init(40000, G726_PACKING_LEFT); }
51 static void
52 codec_g726_release(codec_context_t *ctx)
54 g726_codec_ctx *state = (g726_codec_ctx *)(ctx->priv);
56 if (!state) {
57 return; /* out-of-memory; */
60 /* Note: replaces g726_release since SpanDSP 20090211 */
61 g726_free(state->state);
62 g_free(state);
65 static unsigned
66 codec_g726_get_channels(codec_context_t *ctx _U_)
68 return 1;
71 static unsigned
72 codec_g726_get_frequency(codec_context_t *ctx _U_)
74 return 8000;
77 static size_t
78 codec_g726_decode(codec_context_t *ctx, const void *inputBytes,
79 size_t inputBytesSize, void *outputSamples, size_t *outputSamplesSize)
81 g726_codec_ctx *state = (g726_codec_ctx *)ctx->priv;
83 if (!state) {
84 return 0; /* out-of-memory; */
87 if (!outputSamples || !outputSamplesSize) {
89 * sample rate 8kHz, for bitrate 16kHz we have 16/8 = 2 bits/sample, so
90 * 1 input byte (8 bits) will expand to four 16-bit samples. Likewise,
91 * for bitrate 40kHz we have 40/8 = 5 bits/sample. Alternatively:
92 * bitsPerSample = bitRate / sampleRate (8kHz).
93 * outputBytes = (inputBits / bitsPerSample) * sizeof(sample)
95 return inputBytesSize * 8 / (state->bit_rate / 8000) * 2;
98 /* g726_decode returns the number of 16-bit samples. */
99 *outputSamplesSize = 2 * g726_decode(state->state, (int16_t *)outputSamples, (const uint8_t *) inputBytes, (int)inputBytesSize);
100 return *outputSamplesSize;
103 void
104 codec_register_g726(void)
106 register_codec("G726-16", codec_g726_16_init, codec_g726_release,
107 codec_g726_get_channels, codec_g726_get_frequency, codec_g726_decode);
108 register_codec("G726-24", codec_g726_24_init, codec_g726_release,
109 codec_g726_get_channels, codec_g726_get_frequency, codec_g726_decode);
110 register_codec("G726-32", codec_g726_32_init, codec_g726_release,
111 codec_g726_get_channels, codec_g726_get_frequency, codec_g726_decode);
112 register_codec("G726-40", codec_g726_40_init, codec_g726_release,
113 codec_g726_get_channels, codec_g726_get_frequency, codec_g726_decode);
114 register_codec("AAL2-G726-16", codec_aal2_g726_16_init, codec_g726_release,
115 codec_g726_get_channels, codec_g726_get_frequency, codec_g726_decode);
116 register_codec("AAL2-G726-24", codec_aal2_g726_24_init, codec_g726_release,
117 codec_g726_get_channels, codec_g726_get_frequency, codec_g726_decode);
118 register_codec("AAL2-G726-32", codec_aal2_g726_32_init, codec_g726_release,
119 codec_g726_get_channels, codec_g726_get_frequency, codec_g726_decode);
120 register_codec("AAL2-G726-40", codec_aal2_g726_40_init, codec_g726_release,
121 codec_g726_get_channels, codec_g726_get_frequency, codec_g726_decode);
125 * Editor modelines - https://www.wireshark.org/tools/modelines.html
127 * Local variables:
128 * c-basic-offset: 4
129 * tab-width: 8
130 * indent-tabs-mode: nil
131 * End:
133 * vi: set shiftwidth=4 tabstop=8 expandtab:
134 * :indentSize=4:tabSize=8:noTabs=true: