@@ -13,31 +13,37 @@ namespace audio_tools {
13
13
*/
14
14
class ADPCMDecoder : public AudioDecoderExt {
15
15
public:
16
+ ADPCMDecoder () = default ;
17
+
16
18
ADPCMDecoder (AVCodecID id, int blockSize = ADAPCM_DEFAULT_BLOCK_SIZE) {
17
- if (id == AV_CODEC_ID_ADPCM_IMA_AMV) {
18
- info.sample_rate = 22050 ;
19
- info.channels = 1 ;
20
- info.bits_per_sample = 16 ;
21
- }
22
- p_decoder = adpcm_ffmpeg::ADPCMDecoderFactory::create (id);
19
+ setBlockSize (blockSize);
20
+ setId (id);
21
+ }
22
+
23
+ // / Destructor
24
+ ~ADPCMDecoder () {
25
+ if (p_decoder) delete p_decoder;
26
+ }
27
+
28
+ // (re) defines the codec id: set the block size first
29
+ void setId (AVCodecID id) {
30
+ codec_id = id;
23
31
if (p_decoder != nullptr ) {
24
- p_decoder->setCodecID (id);
25
- p_decoder->setBlockSize (blockSize);
26
- } else {
27
- LOGE (" Decoder not implemented" );
32
+ setImplementation ();
28
33
}
29
34
}
30
35
31
- // defines the block size
36
+ // defines the block size (= size of encoded frame)
32
37
void setBlockSize (int blockSize) override {
38
+ block_size = blockSize;
33
39
if (p_decoder == nullptr ) return ;
34
40
p_decoder->setBlockSize (blockSize);
35
41
}
36
42
37
- // / Provides the block size (size of encoded frame) (only available after
43
+ // / Provides the block size (= size of encoded frame) (only available after
38
44
// / calling begin)
39
45
int blockSize () {
40
- if (p_decoder == nullptr ) return 0 ;
46
+ if (p_decoder == nullptr ) return block_size ;
41
47
return p_decoder->blockSize ();
42
48
}
43
49
@@ -50,7 +56,9 @@ class ADPCMDecoder : public AudioDecoderExt {
50
56
51
57
bool begin () override {
52
58
TRACEI ();
53
- if (p_decoder == nullptr ) return false ;
59
+ if (p_decoder == nullptr ) {
60
+ setImplementation ();
61
+ }
54
62
if (is_started) return true ;
55
63
current_byte = 0 ;
56
64
LOGI (" sample_rate: %d, channels: %d" , info.sample_rate , info.channels );
@@ -98,7 +106,8 @@ class ADPCMDecoder : public AudioDecoderExt {
98
106
Vector<uint8_t > adpcm_block;
99
107
Print *p_print = nullptr ;
100
108
int current_byte = 0 ;
101
- int block_size = 0 ;
109
+ int block_size = ADAPCM_DEFAULT_BLOCK_SIZE;
110
+ AVCodecID codec_id = AV_CODEC_ID_ADPCM_MS;
102
111
bool is_started = false ;
103
112
104
113
virtual bool decode (uint8_t byte) {
@@ -126,6 +135,29 @@ class ADPCMDecoder : public AudioDecoderExt {
126
135
}
127
136
return true ;
128
137
}
138
+
139
+ // / change the decoder implementation
140
+ void setImplementation () {
141
+ // delete the old decoder
142
+ if (p_decoder != nullptr ) {
143
+ p_decoder->end ();
144
+ delete p_decoder;
145
+ p_decoder = nullptr ;
146
+ }
147
+
148
+ if (codec_id == AV_CODEC_ID_ADPCM_IMA_AMV) {
149
+ info.sample_rate = 22050 ;
150
+ info.channels = 1 ;
151
+ info.bits_per_sample = 16 ;
152
+ }
153
+ p_decoder = adpcm_ffmpeg::ADPCMDecoderFactory::create (codec_id);
154
+ if (p_decoder != nullptr ) {
155
+ p_decoder->setCodecID (codec_id);
156
+ p_decoder->setBlockSize (block_size);
157
+ } else {
158
+ LOGE (" Decoder not implemented" );
159
+ }
160
+ }
129
161
};
130
162
131
163
/* *
@@ -137,21 +169,33 @@ class ADPCMDecoder : public AudioDecoderExt {
137
169
*/
138
170
class ADPCMEncoder : public AudioEncoderExt {
139
171
public:
172
+ ADPCMEncoder () = default ;
173
+
140
174
ADPCMEncoder (AVCodecID id, int blockSize = ADAPCM_DEFAULT_BLOCK_SIZE) {
141
- if (id == AV_CODEC_ID_ADPCM_IMA_AMV) {
142
- info.sample_rate = 22050 ;
143
- info.channels = 1 ;
144
- info.bits_per_sample = 16 ;
145
- }
146
- p_encoder = adpcm_ffmpeg::ADPCMEncoderFactory::create (id);
175
+ setId (id);
176
+ setBlockSize (blockSize);
177
+ }
178
+
179
+ // / Destructor
180
+ ~ADPCMEncoder () {
181
+ if (p_encoder != nullptr ) delete p_encoder;
182
+ }
183
+
184
+ // / (re) defines the codec id
185
+ void setId (AVCodecID id) {
186
+ codec_id = id;
147
187
if (p_encoder != nullptr ) {
148
- p_encoder->setCodecID (id);
149
- p_encoder->setBlockSize (blockSize);
150
- } else {
151
- LOGE (" Encoder not implemented" );
188
+ setImplementation ();
152
189
}
153
190
}
154
191
192
+ // / (re) defines the block size
193
+ void setBlockSize (int blockSize) {
194
+ block_size = blockSize;
195
+ if (p_encoder == nullptr ) return ;
196
+ p_encoder->setBlockSize (blockSize);
197
+ }
198
+
155
199
// / Provides the block size (size of encoded frame) (only available after
156
200
// / calling begin)
157
201
int blockSize () override {
@@ -168,7 +212,9 @@ class ADPCMEncoder : public AudioEncoderExt {
168
212
169
213
bool begin () override {
170
214
TRACEI ();
171
- if (p_encoder == nullptr ) return false ;
215
+ if (p_encoder == nullptr ) {
216
+ setImplementation ();
217
+ };
172
218
if (is_started) return true ;
173
219
LOGI (" sample_rate: %d, channels: %d" , info.sample_rate , info.channels );
174
220
p_encoder->begin (info.sample_rate , info.channels );
@@ -208,12 +254,15 @@ class ADPCMEncoder : public AudioEncoderExt {
208
254
}
209
255
210
256
protected:
257
+ AVCodecID codec_id = AV_CODEC_ID_ADPCM_MS;
211
258
adpcm_ffmpeg::ADPCMEncoder *p_encoder = nullptr ;
212
259
Vector<int16_t > pcm_block;
213
260
Print *p_print = nullptr ;
214
261
bool is_started = false ;
215
262
int current_sample = 0 ;
216
263
int total_samples = 0 ;
264
+ int current_id = -1 ;
265
+ int block_size = ADAPCM_DEFAULT_BLOCK_SIZE;
217
266
218
267
virtual bool encode (int16_t sample) {
219
268
if (p_encoder == nullptr ) return false ;
@@ -237,6 +286,29 @@ class ADPCMEncoder : public AudioEncoderExt {
237
286
}
238
287
return true ;
239
288
}
289
+
290
+ // / change the encoder implementation
291
+ bool setImplementation () {
292
+ // delete the old encoder
293
+ if (p_encoder != nullptr ) {
294
+ p_encoder->end ();
295
+ delete p_encoder;
296
+ p_encoder = nullptr ;
297
+ }
298
+
299
+ if (codec_id == AV_CODEC_ID_ADPCM_IMA_AMV) {
300
+ info.sample_rate = 22050 ;
301
+ info.channels = 1 ;
302
+ info.bits_per_sample = 16 ;
303
+ }
304
+ p_encoder = adpcm_ffmpeg::ADPCMEncoderFactory::create (codec_id);
305
+ if (p_encoder != nullptr ) {
306
+ p_encoder->setCodecID (codec_id);
307
+ p_encoder->setBlockSize (block_size);
308
+ } else {
309
+ LOGE (" Encoder not implemented" );
310
+ }
311
+ }
240
312
};
241
313
242
314
} // namespace audio_tools
0 commit comments