3 #include "AudioTools/AudioCodecs/AudioCodecsBase.h"
20 decoder.setCodecID(
id);
25 void setBlockSize(
int blockSize)
override {
31 return decoder.blockSize();
36 return decoder.frameSize()*2;
40 bool begin()
override {
42 if (is_started)
return true;
48 block_size = decoder.blockSize();
49 assert(block_size > 0);
50 assert(decoder.frameSize() > 0);
51 adpcm_block.resize(block_size);
53 notifyAudioChange(info);
61 adpcm_block.resize(0);
65 virtual void setOutput(
Print &out_stream)
override { p_print = &out_stream; }
67 virtual size_t write(
const uint8_t *data,
size_t len)
override {
70 uint8_t *input_buffer8 = (uint8_t *)data;
71 LOGD(
"write: %d", (
int)len);
72 for (
int j = 0; j < len; j++) {
73 decode(input_buffer8[j]);
78 operator bool()
override {
return is_started; }
81 adpcm_ffmpeg::ADPCMDecoder decoder;
82 Vector<uint8_t> adpcm_block;
83 Print *p_print =
nullptr;
86 bool is_started =
false;
88 virtual bool decode(uint8_t
byte) {
89 adpcm_block[current_byte++] = byte;
91 if (current_byte >= block_size) {
93 AVFrame &frame = decoder.decode(&adpcm_block[0], block_size);
95 int16_t *data = (int16_t *)frame.data[0];
96 size_t byte_count = frame.nb_samples *
sizeof(int16_t) * info.
channels;
97 size_t written = p_print->write((uint8_t *)data, byte_count);
98 if (written != byte_count) {
99 LOGE(
"decode %d -> %d -> %d",block_size, (
int)byte_count, (
int)written);
101 LOGD(
"decode %d -> %d -> %d",block_size, (
int)byte_count, (
int)written);
124 encoder.setCodecID(
id);
130 return encoder.blockSize();
135 return encoder.frameSize()*2;
138 bool begin()
override {
140 if (is_started)
return true;
146 assert(encoder.frameSize() != 0);
147 total_samples = encoder.frameSize()*info.
channels;
148 pcm_block.resize(total_samples);
155 void end()
override {
162 const char *
mime()
override {
return "audio/adpcm"; }
164 void setOutput(
Print &out_stream)
override { p_print = &out_stream; }
166 operator bool()
override {
return is_started; }
168 size_t write(
const uint8_t *data,
size_t len)
override {
169 LOGD(
"write: %d", (
int)len);
170 int16_t *data16 = (int16_t *)data;
171 for (
int j = 0; j < len / 2; j++) {
178 adpcm_ffmpeg::ADPCMEncoder encoder;
179 Vector<int16_t> pcm_block;
180 Print *p_print =
nullptr;
181 bool is_started =
false;
182 int current_sample = 0;
185 virtual bool encode(int16_t sample) {
186 pcm_block[current_sample++] = sample;
187 if (current_sample >= total_samples) {
189 AVPacket &packet = encoder.encode(&pcm_block[0], total_samples);
190 if (packet.size > 0) {
191 size_t written = p_print->write(packet.data, packet.size);
192 if (written != packet.size) {
193 LOGE(
"encode %d->%d->%d",2*total_samples, (
int)packet.size, (
int)written);
195 LOGD(
"encode %d->%d->%d",2*total_samples, (
int)packet.size, (
int)written);