1 /* G722decode.c
2  * G.722 codec
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10 
11 #include "config.h"
12 
13 #include <glib.h>
14 
15 #include "spandsp.h"
16 #include "wsutil/codecs.h"
17 #include "ws_attributes.h"
18 
19 void codec_register_g722(void);
20 
21 static void *
codec_g722_init(void)22 codec_g722_init(void)
23 {
24     g722_decode_state_t *state;
25 
26     /* Valid values for bit_rate for G.722 are 48000, 56000, 64000, but RTP/AVP
27      * profile requires 64kbps, aligned at octets. */
28     state = g722_decode_init(NULL, 64000, 0);
29 
30     return state;
31 }
32 
33 static void
codec_g722_release(void * ctx)34 codec_g722_release(void *ctx)
35 {
36     g722_decode_state_t *state = (g722_decode_state_t *)ctx;
37 
38     if (!state) {
39         return;  /* out-of-memory; */
40     }
41 
42     /* Note: replaces g722_decode_release since SpanDSP 20090211 */
43     g722_decode_free(state);
44 }
45 
46 static unsigned
codec_g722_get_channels(void * ctx _U_)47 codec_g722_get_channels(void *ctx _U_)
48 {
49     /* G.722 has only one channel. */
50     return 1;
51 }
52 
53 static unsigned
codec_g722_get_frequency(void * ctx _U_)54 codec_g722_get_frequency(void *ctx _U_)
55 {
56     /* Note: RTP Clock rate is 8kHz due to a historic error, but actual sampling
57      * rate is 16kHz (RFC 3551, section 4.5.2). */
58     return 16000;
59 }
60 
61 static size_t
codec_g722_decode(void * ctx,const void * inputBytes,size_t inputBytesSize,void * outputSamples,size_t * outputSamplesSize)62 codec_g722_decode(void *ctx, const void *inputBytes, size_t inputBytesSize,
63         void *outputSamples, size_t *outputSamplesSize)
64 {
65     g722_decode_state_t *state = (g722_decode_state_t *)ctx;
66 
67     if (!state) {
68         return 0;  /* out-of-memory; */
69     }
70 
71     if (!outputSamples || !outputSamplesSize) {
72         return 4 * inputBytesSize;
73     }
74 
75     /* g722_decode returns the number of 16-bit samples. */
76     *outputSamplesSize = 2 * g722_decode(state, (int16_t *)outputSamples,
77         (const uint8_t *)inputBytes, (int)inputBytesSize);
78     return *outputSamplesSize;
79 }
80 
81 void
codec_register_g722(void)82 codec_register_g722(void)
83 {
84     register_codec("g722", codec_g722_init, codec_g722_release,
85             codec_g722_get_channels, codec_g722_get_frequency, codec_g722_decode);
86 }
87 
88 /*
89  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
90  *
91  * Local variables:
92  * c-basic-offset: 4
93  * tab-width: 8
94  * indent-tabs-mode: nil
95  * End:
96  *
97  * vi: set shiftwidth=4 tabstop=8 expandtab:
98  * :indentSize=4:tabSize=8:noTabs=true:
99  */
100