gnu: emacs-consult: Fix grammar.
[jackhill/guix/guix.git] / gnu / packages / patches / twinkle-bcg729.patch
1 From 46bee14d3cc49d4fb49eaf36a29dbcc7a11a5ab7 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Bri=C3=A8re?= <fbriere@fbriere.net>
3 Date: Sat, 6 Jul 2019 15:40:56 -0400
4 Subject: [PATCH] Add support for the new bcg729 API, introduced in version
5 1.0.2
6
7 Starting with version 1.0.2, bcg729 has changed its API to add support
8 for G.729B, thus requiring us to adjust our function calls depending on
9 which version is installed.
10
11 When dealing with the new API, we merely need to add a few parameters to
12 disable all G.729B features, namely:
13
14 * On the decoder side: When `SIDFrameFlag` is not set, the decoder will
15 behave just like before, decoding the payload as a standard G.729A
16 voice frame (or concealing an erased frame). The other parameters,
17 `rfc3389PayloadFlag` and `bitStreamLength`, are only of use when
18 dealing with a SID frame sent as per RFC 3389, and are ignored if
19 `SIDFrameFlag` is not set.
20
21 * On the encoder side: When `enableVAD` is disabled, the encoder will
22 behave just like before, producing only standard G.729A voice frames.
23 The only API difference is the introduction of `*bitStreamLength`, to
24 return the length of the encoded frame (0, 2 or 10 bytes). In our
25 case, this will always be 10 bytes just like before; an assert() was
26 added to guarantee this.
27
28 Closes #104
29 ---
30 CMakeLists.txt | 4 ++++
31 cmake/FindG729.cmake | 36 ++++++++++++++++++++++++++++++++++++
32 src/audio/audio_decoder.cpp | 8 ++++++++
33 src/audio/audio_encoder.cpp | 10 ++++++++++
34 twinkle_config.h.in | 1 +
35 5 files changed, 59 insertions(+)
36
37 diff --git a/CMakeLists.txt b/CMakeLists.txt
38 index 1dafe55..7e3fde0 100644
39 --- a/CMakeLists.txt
40 +++ b/CMakeLists.txt
41 @@ -116,6 +116,10 @@ if (WITH_G729)
42 if (G729_FOUND)
43 message(STATUS "bcg729 OK")
44 set(HAVE_BCG729 TRUE)
45 +
46 + if (G729_ANNEX_B)
47 + set(HAVE_BCG729_ANNEX_B TRUE)
48 + endif (G729_ANNEX_B)
49
50 include_directories(${G729_INCLUDE_DIR})
51 else (G729_FOUND)
52 diff --git a/cmake/FindG729.cmake b/cmake/FindG729.cmake
53 index 4a30ba0..1fbfeeb 100644
54 --- a/cmake/FindG729.cmake
55 +++ b/cmake/FindG729.cmake
56 @@ -1,14 +1,50 @@
57 +INCLUDE(CMakePushCheckState)
58 +INCLUDE(CheckCSourceCompiles)
59 +
60 FIND_PATH(G729_INCLUDE_DIR bcg729/decoder.h)
61 FIND_LIBRARY(G729_LIBRARY NAMES bcg729)
62
63 IF(G729_INCLUDE_DIR AND G729_LIBRARY)
64 SET(G729_FOUND TRUE)
65 +
66 + # The bcg729 API was changed in 1.0.2 to add support for G.729 Annex B.
67 + # This checks whether we are dealing with the old or new API.
68 + CMAKE_PUSH_CHECK_STATE()
69 + SET(CMAKE_REQUIRED_INCLUDES "${INCLUDE_DIRECTORIES}" "${G729_INCLUDE_DIR}")
70 + SET(CMAKE_REQUIRED_LIBRARIES "${G729_LIBRARY}")
71 + SET(CMAKE_REQUIRED_QUIET TRUE)
72 + # Try to compile something using the old (pre-1.0.2) API.
73 + #
74 + # We cannot do it the other way around, as initBcg729EncoderChannel()
75 + # did not have a prototype before 1.0.2, thus compilation would not fail
76 + # when passing it an extra argument.
77 + CHECK_C_SOURCE_COMPILES("
78 + #include <bcg729/encoder.h>
79 +
80 + int main() {
81 + /* This function requires an argument since 1.0.2 */
82 + initBcg729EncoderChannel();
83 + return 0;
84 + }
85 + " G729_OLD_API)
86 + CMAKE_POP_CHECK_STATE()
87 +
88 + IF (G729_OLD_API)
89 + SET(G729_ANNEX_B FALSE)
90 + ELSE (G729_OLD_API)
91 + SET(G729_ANNEX_B TRUE)
92 + ENDIF (G729_OLD_API)
93 ENDIF(G729_INCLUDE_DIR AND G729_LIBRARY)
94
95 IF(G729_FOUND)
96 IF (NOT G729_FIND_QUIETLY)
97 MESSAGE(STATUS "Found bcg729 includes: ${G729_INCLUDE_DIR}/bcg729/decoder.h")
98 MESSAGE(STATUS "Found bcg729 library: ${G729_LIBRARY}")
99 + IF (G729_ANNEX_B)
100 + MESSAGE(STATUS "bcg729 supports Annex B; using the new (1.0.2) API")
101 + ELSE (G729_ANNEX_B)
102 + MESSAGE(STATUS "bcg729 does not support Annex B; using the old (pre-1.0.2) API")
103 + ENDIF (G729_ANNEX_B)
104 ENDIF (NOT G729_FIND_QUIETLY)
105 ELSE(G729_FOUND)
106 IF (G729_FIND_REQUIRED)
107 diff --git a/src/audio/audio_decoder.cpp b/src/audio/audio_decoder.cpp
108 index 65935dd..c661910 100644
109 --- a/src/audio/audio_decoder.cpp
110 +++ b/src/audio/audio_decoder.cpp
111 @@ -547,7 +547,11 @@ uint16 t_g729a_audio_decoder::decode(uint8 *payload, uint16 payload_size,
112
113 for (uint16 done = 0; done < payload_size; done += 10)
114 {
115 +#ifdef HAVE_BCG729_ANNEX_B
116 + bcg729Decoder(_context, &payload[done], 0, false, false, false, &pcm_buf[done * 8]);
117 +#else
118 bcg729Decoder(_context, &payload[done], false, &pcm_buf[done * 8]);
119 +#endif
120 }
121
122 return payload_size * 8;
123 @@ -562,7 +566,11 @@ uint16 t_g729a_audio_decoder::conceal(int16 *pcm_buf, uint16 pcm_buf_size)
124 {
125 assert(pcm_buf_size >= 80);
126
127 +#ifdef HAVE_BCG729_ANNEX_B
128 + bcg729Decoder(_context, nullptr, 0, true, false, false, pcm_buf);
129 +#else
130 bcg729Decoder(_context, nullptr, true, pcm_buf);
131 +#endif
132 return 80;
133 }
134
135 diff --git a/src/audio/audio_encoder.cpp b/src/audio/audio_encoder.cpp
136 index d6ff356..952b1ee 100644
137 --- a/src/audio/audio_encoder.cpp
138 +++ b/src/audio/audio_encoder.cpp
139 @@ -433,7 +433,11 @@ uint16 t_g726_audio_encoder::encode(int16 *sample_buf, uint16 nsamples,
140 t_g729a_audio_encoder::t_g729a_audio_encoder(uint16 payload_id, uint16 ptime, t_user *user_config)
141 : t_audio_encoder(payload_id, ptime, user_config)
142 {
143 +#ifdef HAVE_BCG729_ANNEX_B
144 + _context = initBcg729EncoderChannel(false);
145 +#else
146 _context = initBcg729EncoderChannel();
147 +#endif
148 }
149
150 t_g729a_audio_encoder::~t_g729a_audio_encoder()
151 @@ -451,7 +455,13 @@ uint16 t_g729a_audio_encoder::encode(int16 *sample_buf, uint16 nsamples,
152
153 for (uint16 done = 0; done < nsamples; done += 80)
154 {
155 +#ifdef HAVE_BCG729_ANNEX_B
156 + uint8 frame_size = 10;
157 + bcg729Encoder(_context, &sample_buf[done], &payload[done / 8], &frame_size);
158 + assert(frame_size == 10);
159 +#else
160 bcg729Encoder(_context, &sample_buf[done], &payload[done / 8]);
161 +#endif
162 }
163
164 return nsamples / 8;
165 diff --git a/twinkle_config.h.in b/twinkle_config.h.in
166 index a1aa3b4..53a0426 100644
167 --- a/twinkle_config.h.in
168 +++ b/twinkle_config.h.in
169 @@ -4,6 +4,7 @@
170 #cmakedefine HAVE_ILBC_CPP
171 #cmakedefine HAVE_ZRTP
172 #cmakedefine HAVE_BCG729
173 +#cmakedefine HAVE_BCG729_ANNEX_B
174 #cmakedefine HAVE_GSM
175
176 #cmakedefine HAVE_UNISTD_H